[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] sequential numbering in xslt
Subject: Re: [xsl] sequential numbering in xslt From: a kusa <akusa8@xxxxxxxxx> Date: Mon, 11 Jan 2010 14:10:21 -0600 |
Hi I tried to implement this solution for my program. Here is what I did. I have a folder with xml files. I used collection() to point to that folder. But the problem I am having is that reading the xml files one by one. When I say $document[1], it considers all files as $documents[1]. So my output files end up like this: If I have 2 XML files inside the folder and I run my trasnformation using saxon9.1, I get back the two xml files as output but the @seq is as shown: 1st XML file; <car seq="12"> <req>....<req> <body id="c1"> </body> </car> 2nd XML file: <car seq="12"> <req>....<req> <body id="c1"> </body> </car> Can you please tell me what I am doing wrong here? On Mon, Jan 4, 2010 at 11:52 AM, James A. Robinson <jim.robinson@xxxxxxxxxxxx> wrote: > >> 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)
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] sequential numbering in x, Michael Kay | Thread | Re: [xsl] sequential numbering in x, David Carlisle |
Re: [xsl] Definite list of XSLT 2.0, Andrew Welch | Date | Re: [xsl] sequential numbering in x, David Carlisle |
Month |