[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: sequential numbering in xslt
> another XML format using XSLT. The output XML file has a schema and > has a required 'seq' attribute in the root element that needs to be > incremented for each input XML file. > ... > for every input file, in the transformation, I want to increment > attribute 'seq' in the output file by 1 when I transform the input > files using xslt. > I have tried <xsl:number> and tried writing a function. But the > problem is that since variables in xslt are constants, there is no way > to increment a number, store it in a temp variable and increment it > for the next time in xslt 2.0 unlike procedural languages like C or > C++. > > Is there any other way of achieving this in XSLT 2.0? Using position() is one option, keeping track of the number of documents you are processing yourself, using a recursive template, is another: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <!-- URI approporiate for fn:collection, e.g., in saxon: <collection stable="true"> <doc href="file1.xml" /> <doc href="file2.xml" /> <doc href="file3.xml" /> </collection> --> <xsl:param name="collection-href" required="yes" /> <!-- Previous sequence number, if we need to start at some point other than 1. --> <xsl:param name="previous-number" select="0" /> <xsl:template match="/"> <xsl:call-template name="example"> <xsl:with-param name="documents" select="collection($collection-href)" /> <xsl:with-param name="previous-number" select="$previous-number" /> </xsl:call-template> </xsl:template> <xsl:template name="example"> <xsl:param name="documents" /> <xsl:param name="previous-number" required="yes" /> <xsl:if test="$documents[1]"> <xsl:variable name="current-number" as="xs:integer" select="xs:integer($previous-number+1)" /> <!-- Here we could use xsl:result-document and xsl:apply-templates to kick off processing of our input document. --> <example seq="{$current-number}" /> <!-- Now we recursively process the next document in our list --> <xsl:call-template name="example"> <xsl:with-param name="documents" select="remove($documents, 1)" /> <xsl:with-param name="previous-number" select="$current-number" /> </xsl:call-template> </xsl:if> <!-- Here, if not($documents), we could write out our last number to some file that we use to track our sequence number across time. --> </xsl:template> </xsl:stylesheet> Running this w/ the example collection document and no override of $previous-number would produce <example seq="1"/> <example seq="2"/> <example seq="3"/> whereas setting $previous-number to 3 would produce <example seq="4"/> <example seq="5"/> <example seq="6"/> Jim - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - James A. Robinson jim.robinson@xxxxxxxxxxxx Stanford University HighWire Press http://highwire.stanford.edu/ +1 650 7237294 (Work) +1 650 7259335 (Fax)
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|