tags:

views:

329

answers:

3

I've got a question about loading attribute in XPATH. I write short XML code to test:

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="testDate.xsl"?>
<element attribute="1/1/2100">
  Hung
 </element>

My XSL code:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <!--Handle the document: set up HTML page-->
  <xsl:template match="/">
    <html>
    <head>    
    </head>
    <body>
   This is a test   
     <xsl:value-of select="element@attribute"/>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Why it produces an error when loading the stylesheet? Would you please help me explain this? Thank you

+2  A: 

I suspect your XPath for attribute is wrong. I think it should be

element/@attribute

i.e. you should separate element and @attribute with a /

Brian Agnew
+3  A: 

You need to put a slash before the @ in your <xsl:value-of />.

You're getting an error because element@attribute is not valid XPath. Putting the slash in indicates that you want to:

  • find elements called element, and then
  • within these elements, find an attribute called attribute.

The following amended stylesheet works for me:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <!--Handle the document: set up HTML page-->
  <xsl:template match="/">
    <html>
    <head>    
    </head>
    <body>
                This is a test                  
        <xsl:value-of select="element/@attribute"/>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Pourquoi Litytestdata
A: 

It works, thanks a lot!

Nguyen Quoc Hung