2
votes

I have searched many posts on dynamically setting namespaces but they all seem to refer to setting the namespace of the output XML.

The issue I have is that the namespace (defined at root and same for all child nodes) of the input XML differs, and the same stylesheet needs to be able to handle both inputs.

For example, one input XML is:

<root xmlns="aaa">
    <body>xxx</body>
</root>

And the other input XML is:

<root xmlns="bbb">
    <body>yyy</body>
</root>

In the stylesheet, my XPath obviously needs to use the defined namespace, which is declared with a prefix, i.e.:

xmlns:one="aaa"

But as soon as the second input XML is being transformed, it of course fails to work.

I could define another namespace, e.g.

xmlns:two="bbb"

But the only way to use that namespace at the right time is to duplicate all the XSLT code and have the other namespace as the prefix for all the XPath (even then I would still need to identify which set of XPath to use which may be fun..)

My stylesheet currently uses the following XPath:

%lt;SOMETHING&gt;
<xsl:value-of select="one:body" />
%lt;/SOMETHING&gt;

As you can see it uses the "one" namespace prefix. Is there any way to just get the value of either "body" tag, regardless of namespace? As mentioned in a comment below, although I appreciate they are different elements based on namespace, I know that the information in each will be the same so I can treat them as such.

I have seen posts on using xsl:element with a namespace attribute but from what I can tell that just defines the namespace of the output XML, not the input. (To make matters worse, what I am outputting is actually escaped XML, e.g. %lt;SOMETHING> so I couldn't use xsl:element anyway).

My current solution (since posting this) is to have two extra stylesheets included in the main stylesheet. Each one is specific to either namespace "one" or namespace "two", each line of XPath uses the relevant namespace prefix.

I am hoping there is a way to avoid having two separate stylesheets that are almost identical except for the namespace prefix.

Thanks in advance.

3
Apart from showing the input documents, please also show the output you expect from the transformation. - Mathias Müller

3 Answers

2
votes

If I get you right, you want to process the XML ignoring the elements' namespaces. Actually, the sense of namespaces is to distinct between elements from different contexts. So from an XML point of view, <one:body> has absolutely nothing to do with <two:body>, besides the fact that they happen to have the same name.

If you want to do it anyway, instead of:

<xsl:template match="one:body">
<xsl:template match="two:body">

you should match on the elements' local name only:

<xsl:template match="*[local-name()='body']">

In order to give a little more background: If you say

<xsl:template match="one:body">

then this is only a short notation of

<xsl:template match="*[namespace-uri()='aaa'][local-name()='body']">

(i.e. "match any element whose namespace is 'aaa' and whose name is 'body'")

Thus, ignoring the namespace by leaving away the

[namespace-uri()='aaa']

makes it

<xsl:template match="*[local-name()='body']">

Instead, you had better say

<xsl:template match="*[namespace-uri()='aaa' or namespace-uri()='bbb'][local-name()='body']">

or

<xsl:template match="*[namespace-uri()='aaa' or namespace-uri()='bbb' or namespace-uri()='ccc'][local-name()='body']">

and so on. If, as dret states, you know all possible namespaces in advance.

1
votes

I would suggest you define both namespaces, then use paths such as:

one:body | two:body

to address the elements in the source XML.


For example, instead of:

<xsl:value-of select="one:body" />

use:

<xsl:value-of select="one:body | two:body" />
0
votes

As I already wrote! Instead of

<xsl:value-of select="one:body" />

you can write

<xsl:value-of select="*[local-name()='body']" />

Or, if you have XPath 2.0, then

<xsl:value-of select="*:body" />