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!
  • Students Click Here

*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.

Students Click Here



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"
        <Line>Hello world</Line>
        <on:SecondLine>Bye bye</on:SecondLine>

Which you tried to process with a stylesheet like this:

<xsl:stylesheet version="1.0"

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

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

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

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

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

Good luck!

Jordi Reineman

Back to XML FAQ Index
Back to XML Forum

My Archive

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