Roy
Roy

Reputation: 45

XSLT select nodes with namespace

I am trying to obtain the values of few tags from a xml using xslt. My list.xml:

<a>
 <na:Data xmlns:na="http://some_site.com#" Ref="http://another_site.com" 
  Key="value">
  <b>
    <c>some_c_attrib</c>
    <d>some_d_attrib</d>
    <e>some_e_attrib</e>
    <f>some_f_attrib</f>
    <g>some_g_attrib</g>
  </b>
  <h>
   <i>some_i_attrib</i>
   <j>some_j_attrib</j>
  </h>
 </na:Data>
 <da:Newtag xmlns:da="http://new_site.com">
   <k name="http://new_new_site.com"/>
 </da:Newtag>
</a>

My list.xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:my="http://some_site.com#"
    exclude-result-prefixes="my">
    <xsl:output method="html" encoding="UTF-8" />
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="/a/my:Data/my:e">
                  <h1><xsl:value-of select="f" /></h1>
                </xsl:for-each>
            </body>
        </html> 
    </xsl:template>
</xsl:stylesheet>

The output which I get is:

<html>
  <body>
    <h1/>
  </body>
</html>

I want the output to be :

<html>
  <body>
    <h1>some_f_attrib</h1>
  </body>
</html>

Likewise I also want to get the attribute values for c,d,e etc. The namespace is creating some problem. Without namespace I am able to access the attributes values. I guess I am going wrong in tha for-each and value-of select statements.

Thanks

Upvotes: 2

Views: 1688

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

There are two issues.

  1. First, the f element is a child of b, not e.
  2. Even if it were a child of e, the e element is not bound to a namespace, and your XPath is attempting to address my:e.

Adjust the XPath in your for-each to:

<xsl:for-each select="/a/my:Data/b">
  <h1><xsl:value-of select="f" /></h1>
</xsl:for-each>

Upvotes: 2

Related Questions