Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to match nodes if there's a default namespace in the xml

XSL

How to match nodes if there's a default namespace in the xml

by  JJR  Posted    (Edited  )
Did you ever come across an xml file which looked something like this:
Code:
<MyXML xmlns="http://www.MyXML.com/ns"
       xmlns:on="http://www.someothernamespace">
    <MyFirstTag>
        <Line>Hello world</Line>
        <on:SecondLine>Bye bye</on:SecondLine>
    </MyFirstTag>
</MyXML>
Which you tried to process with a stylesheet like this:
Code:
<xsl:stylesheet version="1.0"
     xmlns="http://www.MyXML.com/ns"
     xmlns:on="http://www.someothernamespace">

  <xsl:template match="/">
    <html><head/><body>
       <xsl:apply-templates select="MyFirstTag"/>
    </body></html>
  </xsl:template>

  <xsl:template match="MyFirstTag">
    <xsl:value-of select="Line"/>
  </xsl:template> 
</xsl:stylesheet>
But all it produced was a blank html page and not the words "Hello world". Well, you're not the only one!

If the xml data you are processing with your stylesheet contains a default namespace, that is, a namespace without a prefix, you can't just match these nodes in your stylesheet as you would normally do.

According to the requirements document for xslt 2.0, you have to assign a non-null prefix to the default namespace in your xsl and use this prefix in your xpath expressions:


2.1 Must Allow Matching on Default Namespace Without Explicit Prefix

Many users stumble trying to match an element with a default namespace. They expect to be able to do something like:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:myuri">
<!-- Expect this matches <foo> in default namespace -->
<xsl:template match="foo">

thinking that leaving off the prefix from the foo element name, that it will match <foo> elements in the default namespace with the URI of urn:myuri. Instead, they are required to assign a non-null prefix name to their namespace URI and then match on "someprefix:foo" instead, which has proven to be far from obvious . XSLT 2.0 SHOULD provide an explicit way to handle this scenario to avoid further user confusion.

So, in order to create the output: "Hello world" from our xml file, all we have to do is assign the prefix!

Code:
<xsl:stylesheet version="1.0"
     xmlns:someprefix="http://www.MyXML.com/ns"
     xmlns:on="http://www.someothernamespace">

  <xsl:template match="/">
    <html><head/><body>
       <xsl:apply-templates select="someprefix:MyFirstTag"/>
    </body></html>
  </xsl:template>

  <xsl:template match="someprefix:MyFirstTag">
    <xsl:value-of select="someprefix:Line"/>
  </xsl:template> 
</xsl:stylesheet>


Good luck!

Jordi Reineman
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top