determining number of tags in xsl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • persiankamran
    New Member
    • Jul 2008
    • 1

    determining number of tags in xsl

    folks i have the following xml file

    Code:
    <?xml version="1.0" version="utf-8"?> 
    <main>
     <book>
     <author>A1</author> 
    <author>A2</author> 
    <year>2000</year>
     </book> 
    
    <book> 
    <author>b1</author>
     <author>b2</author>
     <author>b3</author>
     <year>2001</year>
     </book>
    
     </main>
    Now i want to transform it using XSL but problem is, in some book tags, number of authors is 2, in other number of authors is 3... how can i do in XSL so that i dont have to hardcoat it e-g


    Code:
    <xsl:for-each select="main/book"> <xsl:sort select="year"/> <p>Author::<xsl:value-of select="author"/></p> <p>Year::<xsl:value-of select="year"/></p>

    The above code returns the first author only.. if i do
    Code:
    <p>Author::<xsl:value-of select="author[1]"/></p> <p>Author::<xsl:value-of select="author[2]"/></p> <p>Author::<xsl:value-of select="author[3]"/></p>
    It becomes hardcoated and rong as well since some books have 3 authors and some have 2,.. is there any way to do it better?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    in this case XPath axes are a valuable means

    Code:
    <xsl:for-each select="main/book">
      <xsl:sort select="year"/>
      <xsl:for-each select="child::author">
        <p>Author::<xsl:value-of select="."/></p>
      </xsl:for-each>
      <p>Year::<xsl:value-of select="year"/></p>
    </xsl:for-each>

    Comment

    Working...