xsl-list
[Top] [All Lists]

Re: [xsl] sequential numbering in xslt

2010-01-03 14:50:04
At 2010-01-03 14:41 -0600, a kusa wrote:
 I have a number of XML files as input, and I transform them into
another XML format using XSLT.
...
So, for example, if I had 5 input xml files a.xml, b.xml, c.xml,
d.xml, e.xml, and I transformed them into Aout.xml, Bout.xml,
Cout.xml, Dout.xml and Eout.xml ,

How are you accessing those 5 input files?

the output of which looks something
like below:
Output XML:

<car seq="1">
<req>....<req>

<body id="c1">

</body>

</car>

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.

If you are accessing them in a for-each construct, then the position() function gives you what you need:

I have tried <xsl:number>

That is only for use in a single tree.

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++.

Correct, but you have blinders on thinking that is the way to approach the problem.

Is there any other way of achieving this in XSLT 2.0?

It depends on how you are accessing your 5 input files.

The following would work if this is the way you are doing it:

  <xsl:for-each select="('a.xml','b.xml','c.xml','d.xml','e.xml')">
    <xsl:result-document href="{replace(.,'\.','out.')}">
      <car seq="{position()}">
        <xsl:apply-templates select="doc(.)"/>
        ...

Or something like:

  <xsl:for-each select="('a.xml','b.xml','c.xml','d.xml','e.xml')">
    <xsl:variable name="seq" select="position()"/>
    <xsl:result-document href="{replace(.,'\.','out.')}">
      <xsl:for=each select="doc(.)">
        <car seq="{$seq}">
        ...

(both untested, so please forgive any typos)

I hope this helps.

. . . . . . . . . . Ken

--
UBL and Code List training:      Copenhagen, Denmark 2010-02-08/10
XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19
XSLT/XQuery/XPath training:   San Carlos, California 2010-04-26/30
Vote for your XML training:   http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--