Waslap
Waslap

Reputation: 571

XSLT namespace versions

I am using XSLT 1.0. Is there a way to deal with input document namespace versions ?

For instance, I used to have input XML

<?xml version="1.0" encoding="UTF-8"?>
<abc:TheTag xmlns:abc="http://example.com/xmls/2014">
    <abc:hello>world</abc:hello>
</abc:TheTag>

with XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:abc="http://example.com/xmls/2014">
<xsl:template match="/">
    <xsl:value-of select="abc:TheTag/abc:hello"/>
</xsl:template>
</xsl:stylesheet>

Now the input changed to

<?xml version="1.0" encoding="UTF-8"?>
<abc:TheTag xmlns:abc="http://example.com/xmls/2016">
    <abc:hello>world</abc:hello>
</abc:TheTag>

i.e. 2014 to 2016

I have to deal with both versions of the input XML though and the parts my style sheet addresses are common to both 2014 and 2016 i.e. the style sheet it self needs no change.

So, is there a way to use one style sheet to work with both versions of the input ?

BTW: XSLT 2.0 makes it simpler by introducing *:TheTag/*:hello but migrating to version 2.0 is a last option for me as it means moving to a new XSLT processor altogether

Upvotes: 0

Views: 159

Answers (2)

Stefan Hegny
Stefan Hegny

Reputation: 2177

If you want it really generic (complicated) and are sure that no clashes occur, you could also use

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

or

  <xsl:value-of select="*[local-name() = 'TheTag' and starts-with(namespace-uri(),
 'http://example.com/xmls/2')]/*[local-name() = 'hello' and 
  starts-with(namespace-uri(),'http://example.com/xmls/2')]"/>

(untested code)

Edit - there was a slash missing in the first code line

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116957

I would do it this way:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns14="http://example.com/xmls/2014"
xmlns:ns16="http://example.com/xmls/2016">

<xsl:template match="/">
    <xsl:value-of select="ns14:TheTag/ns14:hello | ns16:TheTag/ns16:hello"/>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions