вторник, август 31, 2010

XSLT match elements with namespace (XPath)

Example XML file where all elements have (default) namespace:
<rootnode xmlns="http://www.mydomain.com/2010">
 <myelement>
  <title>title of element 1</title>
  <uri>http://example.com</uri>
 </myelement>
 <myelement>
  <title>title of element 2</title>
  <uri>http://example.com</uri>
 </myelement>
</rootnode>
Example XSL file matching elements from the XML file above:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:customNS="http://mydomain.com/2010">
 <xsl:template match="/">
  <xsl:for-each select="customNS:rootnode/customNS:myelement">
  <entry>
   <entrytitle><xsl:value-of select="customNS:title"/></entrytitle>
   <entryurl><xsl:value-of select="customNS:uri"/></entryurl>
  </entry>
  </xsl:for-each>  
 </xsl:template>
</xsl:transform>
The namespace from the XML file should be declared in the XSL file and should be given a name (e.g. customNS), so its elements can be referenced from the XSL file.

Няма коментари: