xsl-list
[Top] [All Lists]

RE: how to select elements in a given namespace?

2003-03-17 13:38:08
  from tidwell's book, if i have an excerpt from an XML file:

  <auth:author xmlns:auth=...>
   <last-name>...
   <first-name>...
   <nationality>...
   <year-of-birth>...
   <year-of-death>...
  </auth:author>

if i want to select all nodes in a given namespace ("auth"), it
appears i can use:

  //namespace::auth

Ummm... no, I believe that will select all namespace nodes named "auth" in the
document, i.e. those namespace nodes with the "auth" prefix.
There are six of them, one on each element node.

To select all nodes in the "auth" namespace, you can use
  //*[namespace-uri() = 'http://whatever.auth.org']
i.e. use the namespace URI itself.

Or, declare the auth: prefix in the stylesheet and use it:
  <xsl:stylesheet ...
        xmlns:auth="http://whatever.auth.org'>
  ...
    select="//auth:*"

This guarantees the selected nodes are in the specified
namespace URI; it doesn't guarantee the prefix used.

By the way, there is only one element in the "auth" namespace in your
example: the <author> element.  The others are in no namespace.

and while xpathtester refuses to highlight the matches, it 
does at least
tell me there were 6 of them.  i'm assuming that these matches are the
auth:author element itself, and the 5 children, right?

No, it matches namespace nodes; see above.
 
  so what is the XPath expression to match, say, the 
<last-name> elements
within this namespace?

  <xsl:stylesheet ...
        xmlns:auth="http://whatever.auth.org'>
  ...
     select="//auth:last-name"


But as mentioned above, there is no such node in your XML fragment.
The <last-name> element is in no namespace, unless you've declared
a default namespace somewhere.

Lars


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list