Accessing elements with namespaces

J

johkar

Given that all the nodes in my XML document have the "dsml" namespace,
what is the XSL syntax to access the elements or apply templates?
Below is a snippet:

<dsml:batchResponse xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core"
xmlns="urn:eek:asis:names:tc:DSML:2:0:core" xmlns:xsd="http://www.w3.org/
2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dsml:searchResponse>
<dsml:searchResultEntry>
<dsml:attr name="mail">
<dsml:value>[email protected]</dsml:value>
</dsml:attr>
</dsml:searchResultEntry>
</dsml:batchResponse>
 
M

Martin Honnen

johkar said:
Given that all the nodes in my XML document have the "dsml" namespace,
what is the XSL syntax to access the elements or apply templates?
Below is a snippet:

<dsml:batchResponse xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core"
xmlns="urn:eek:asis:names:tc:DSML:2:0:core" xmlns:xsd="http://www.w3.org/
2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dsml:searchResponse>
<dsml:searchResultEntry>
<dsml:attr name="mail">
<dsml:value>[email protected]</dsml:value>
</dsml:attr>
</dsml:searchResultEntry>
</dsml:batchResponse>

In the XSLT stylesheet you need to bind a prefix to the namespace URI
and use that prefix in XPath expressions and match patterns. Often the
same prefix as in the XML input is used but that is not necessary.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core">

<xsl:template match="dsml:batchResponse">
...
</xsl:template>

</xsl:stylesheet>
 
J

Joseph Kesselman

johkar said:
Given that all the nodes in my XML document have the "dsml" namespace,
what is the XSL syntax to access the elements or apply templates?

Define a prefix in your stylesheet which maps to that namespace, and use
that prefix in your XPaths, like so:

<xsl:stylesheet ... xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core" ...>
...
<xsl:template match="dsml:batchResponse">
<xsl:apply-templates select="//dsml:value"/>
</xsl:template>
...

<xsl:stylesheet>

and so on.

Standard pointer to the many tutorials/articles on
http://www.ibm.com/xml, since this is a question that any decent XSLT
tutorial should have answered.
 
J

johkar

In the XSLT stylesheet you need to bind a prefix to the namespace URI
and use that prefix in XPath expressions and match patterns. Often the
same prefix as in the XML input is used but that is not necessary.
   <xsl:stylesheet
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0"
     xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core">

     <xsl:template match="dsml:batchResponse">
        ...
     </xsl:template>

   </xsl:stylesheet>

Thanks very much...and quick too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top