variable in xslt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saran3b2
    New Member
    • Aug 2007
    • 19

    #1

    variable in xslt

    hi friends

    i am new user.

    i am in the process of collecting the main content and number of title from the input XML with XSLT.
    I have doubt in variable increament. how to increamtent variable ?

    this is my xml:

    Code:
    <chunk-meta><title km="Pt.Ttl">PT</title></chunk-meta>
    <chunk-meta><title km="At.Ttl">no need</title></chunk-meta>
    <float><meta><title km="Ch.Ttl">CT</title></meta></float>
    <float><meta><title km="N.Ttl">no need</title></meta></float>
    <float-meta><meta><title km="H1.Ttl">HT</title></meta></float-meta>
    i expect output to be like this:

    Code:
    <title km="Pt.Ttl">PT</title><no>1</no>
    <title km="Ch.Ttl">CT</title><no>2</no>
    <title km="H1.Ttl">HT</title><no>3</no>


    this is my xslt:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
    <xsl:variable name="a" select="1"/>
    <xsl:for-each select="//">
    <xsl:if test="title/@km[contains(.,'Pt.T')]">
    <xsl:copy-of select="title"/><no><xsl:copy-of select="$a"/></xsl:if>
    <xsl:if test="title/@km[contains(.,'Ch.Ttl')]"><xsl:variable name="b" select="$a+1"/>
    <xsl:copy-of select="title"/><no><xsl:copy-of select="$b"/></xsl:if>
    <xsl:if test="title/@km[contains(.,'H1.Ttl')]">
    <xsl:variable name="c" select="$b+1"/><xsl:copy-of select="title"/><no><xsl:copy-of select="$c"/></xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>


    thanks in adv

    saran
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I would suggest seperating these particular elements out in order to make them easier to count. Then use the position function to easily get their number.
    [code=xml]
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="/">
    <xsl:for-each select="//title[.!='no need']">
    <xsl:copy-of select="."/>
    <no><xsl:valu e-of select="positio n()"/></no>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    [/code]

    Comment

    Working...