Subject: XSLT: unexpected result
Posted By: saurabh0 Post Date: 11/2/2004 9:04:56 AM
I am trying to transform this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="b.xsl"?>
<aa>
   <bb>111</bb>
   <bb>222</bb>
   <bb>333</bb>
</aa>


Using this xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
      <xsl:for-each select="aa">
        <xsl:value-of select="bb"/><br/>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>


and I am getting this output:
111

Whereas I was expecting:
111
222
333


Where am I going wrong ?

Reply By: joefawcett Reply Date: 11/2/2004 9:38:58 AM
First you loop over all the aa elements, there's only one and can never be more as then your XML would be malformed. You then ask for the value of all the bb elements. In XSLT 1 the value is the string value of the first node. You either want:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:for-each select="aa/bb">
      <xsl:value-of select="."/><br/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

or

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="bb">
    <xsl:value-of select="."/><br/>
  </xsl:template>
</xsl:stylesheet>


--

Joe (Co-author Beginning XML, 3rd edition)
Reply By: saurabh0 Reply Date: 11/2/2004 10:45:12 AM
It worked, thanks a lot! :D

Reply By: saurabh0 Reply Date: 11/2/2004 10:47:10 AM
I want to learn XSLT, could you recommend me a book ?

Reply By: joefawcett Reply Date: 11/2/2004 12:21:44 PM
Well in some ways it's a bad time to learn from books as the new specification is out for version 2 but not many processors support it. Anything by Michael Kay will be good, your best bet is probably his latest work XSLT 2.0 Programmer's Reference, 3rd Edition.
It has all the new stuff but also indicates what is new so you can just use version one if you need to.
The original edition is where I learned from.




--

Joe (Co-author Beginning XML, 3rd edition)

Go to topic 910

Return to index page 726
Return to index page 725
Return to index page 724
Return to index page 723
Return to index page 722
Return to index page 721
Return to index page 720
Return to index page 719
Return to index page 718
Return to index page 717