INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XSL

How to match nodes if there's a default namespace in the xml by JJR
Posted: 22 Oct 01

Did you ever come across an xml file which looked something like this:

<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:

<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!


<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

Back to XML FAQ Index
Back to XML Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close