开发者

XSLT transformation with dynamic namespace

I need to transform an XML file to another XML file, where the source file has a dynamic namespace set to xmlns="whatever". My XSLT runs fine without the namespace being in the file, but I get no output with the namespace. How can I cause the schema of the source file to be applied to the destination fi开发者_如何转开发le?

All help is appreciated and thanks in advance!

EDIT:

I'm trying to copy the namespace uri over to the resulting file:

<xsl:param name="schema">
    <xsl:value-of select="namespace-uri()" />
</xsl:param>

<xsl:element name="root" namespace="$schema">

I have verified that schema is holding the correct value, but the problem is that the program appears to take this too literally:

<root xmlns="$schema">

Is this the right way to go about this?

EDIT x2:

I've implemented Alejandro's suggestion of:

<xsl:element name="root" namespace="{$schema}"/> 

And that works for the most part, except for the fact that I have to put the namespace on every element or else I get the following structure in the result:

<root xmlns="NAMESPACE">
    <foo xmlns="">

etc.

Is there a way to blanket all of the elements with this namespace, other than putting namespace={$schema} on every single line? Bounty and accept for the best answer!

EDIT x3: Better example:

If I do:

<xsl:element name="root" namespace="{namespace-uri()}>
  <xsl:element name="foo">
    <xsl:element name="bar">
    <!--etc-->
    </xsl:element>
  </xsl:element>
</xsl:element>

I get:

<root xmlns="NAMESPACE">
  <foo xmlns="">
    <bar>
    <!--etc-->
    </bar>
  </foo>
<root>

I would like to have them all under namespace NAMESPACE, so I did:

<xsl:element name="root" namespace="{namespace-uri()}>
  <xsl:element name="foo" namespace="{namespace-uri()}>
    <xsl:element name="bar" namespace="{namespace-uri()}>
    <!--etc-->
    </xsl:element>
  </xsl:element>
</xsl:element>

However this is ugly and tedious to type. Is there an easier way to blanket the namespace over all elements? (hopefully this clarifies what I need)


Suppose you have this XML input:

<root xmlns="survivors"> 
  <louis/> 
  <francis/> 
</root> 

Meaning that every element is under default namespace wich its URI is "survivors".

As Welbog wrote you can select francis element with:

/*/*[local-name()='francis']

or

/*[local-name()='root']/*[local-name()='francis']

But, that also select francis element from these XML inputs:

<root xmlns="survivors" xmlns:n="no-survivors"> 
  <louis/> 
  <n:francis/> 
</root> 

or

<root xmlns="survivors"> 
  <louis/> 
  <francis xmlns="no-survivors"/> 
</root> 

You could also strengthen the predicate with some namespace URI. But, wich one? An option could be the default namespace for root element like:

/*/*[local-name()='francis'][namespace-uri()=namespace-uri(/*)]

Surely this make XPath expression very verbose.

In XSLT 2.0 you could use xsl:xpath-default-namespace attribute like:

<xsl:value-of select="/root/francis" xpath-default-namespace="survivors"/> 

But that's not good for your case because you don't know the URI in advance.

EDIT: xsl:element 's attributes are AVT (Attribute Value Template) so you need this:

<xsl:element name="root" namespace="{$schema}"/> 

Also, I recomend you to declare the param as a string data type (not RTF like now), something like:

<xsl:param name="schema" select="namespace-uri()"/> 

EDIT 2: Maybe I was not clear. You don't need xsl:element/@namespace in every case. Following your statement that every element is in only one default namespace, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[local-name()='bar']">
        <xsl:element name="newbar" namespace="{namespace-uri()}">
                <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

With this input:

<root xmlns="whatever">
    <foo/>
    <bar/>
</root>

Output:

<root xmlns="whatever">
    <foo></foo>
    <newbar></newbar>
</root>

Edit 2: I was showing you that when you are copying an element you are also copying the namespace needed for expand the QName. So, if want to transform this:

<root xmlns="whatever">
    <foo/>
    <bar/>
</root>

Into this:

<root xmlns="whatever">
    <foo>
        <bar/>
    </foo>
</root> 

You can use this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="*[1]|following-sibling::*[1]"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>


Since the namespace is part of the full name of any elements your XPath is referencing, and since you don't know the namespace of the source file in advance, you'll have to use the local names of the elements you're accessing instead of their full names.

Let's say you have a source file like this:

<root xmlns="survivors">
  <louis/>
  <francis/>
</root>

One way to access these elements using XSLT is to specify a namespace that matches the source file's default namespace:

<xsl:stylesheet
  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:surv="survivors"
>
  <xsl:template match="surv:louis">
    <!-- ETC -->

That way works when you know the namespace. When you don't know the namespace, you can ignore it using the XPath function local-name() like this:

<xsl:stylesheet
  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:template match="*[local-name() = 'louis']">
    <!-- ETC -->

Using local-name() means you can ignore the namespace in the source document. You'll have to be careful if there are multiple elements with the same local name in different namespace, though. It's not exactly a robust solution, but if you can't trust your namespace then you don't have that many options anyway.

I'd imagine that having a variable namespace is a bigger problem in and of itself. If that's under your control, you should correct it. If it's not under your control, you should push to have it corrected.


XSLT is rarely executed in a vaccum. It's almost always a part of some other application that loads the XSLT, loads the source document, and then produces the output.

Assuming the above is your scenario, I wouldn't solve this problem directly with XSLT. Instead, I'd use standard XML technologies to examine the source XML document and discover the default namespace. Then, I'd load the XSLT document and use string substitution or some other technique to inject the resultant namespace at the appropriate point in the transform. Then I'd go ahead and run the transform normally.

This will make your XSLT much more natural to write and maintain. I'm a pro with XSLT and use it constantly, and this is how I would solve it. I couldn't imagine the ugliness of a stylesheet that had to use local-name() comparisons constantly. What a pain. (Of course, in much more complex scenarios there may be no choice. Fortunately yours isn't one of them.)

If you don't have this option, I sympathize.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜