tags:

views:

59

answers:

4

Hi,

I have an XSLT script which I would like to number things sequentially each time I call a template. So a very abbreviated version of it looks a bit like:

<xsl:call-template name="insertHeader" />
<xsl:for-each ...>  
    <xsl:call-template name="insertHeader" />
    ...
</xsl:for-each>
<xsl:call-template name="insertHeader" />

<xsl:template name="insertHeader>
    This is item number <xsl:value-of select="$numberOfInvocations />
</xsl:template>

So obviously that $numberOfInvocations thing doesn't work, and in XSLT you can't increment a global counter variable which would seem like an obvious approach in a procedural language. I would like it to print out 1 the first time that template is called, 2 the second, etc. How should I go about doing this? Is this even remotely possible in XSLT?

Thanks :)

Edit: So there have been a couple of comments that this isn't clearly defined enough. What I want to do is label a series of tables in the (HTML) output. The most obvious way I see of doing this is to call a function (you can probably tell that I am not an XSLT wizard here) that will automatically increment the number each time. I think the reason this seems so hard is because it's the XSLT itself that defines where these tables appear rather than the input.

This extra info may not be of that much use though since Dimitre's answer makes it sound rather like this is never going to work. Thanks anyway :)

A: 

Did you try the position() function?

Here's a snippet from one of my projects, maybe it's helpful:

<xsl:variable name="count" select="count(../ownedParameter[@name])" />
$<xsl:value-of select="@name" />=null
<xsl:if test="$count > 1 and position()!=last()">,</xsl:if>

You should be able to do something like:

<xsl:template name="insertHeader>
    This is item number <xsl:value-of select="position()" />
</xsl:template>

...at least I think that will work. If not, you may need to put the call to count() within a for-each block.

no
I thought (and I might well be wrong, I'm not an expert in matters of xslt) position() only gives the position in a for-each loop; it's not going to keep counting outside that loop?
Peter
I think you're right; was just adding that to my answer. You should still be able to call the template with position() as a parameter.
no
@Peter: You're right. See my answer that use `position()` too but given as a parameter to the template.
dolmen
+1  A: 

You could do this in XSLT 1.0 by using a recursive template.

But, and this is a big but, the name of the template, insertHeader, leads me to think that you're trying to do something that should be solved in a completly different way in XSLT.

What are your intentions with this? Maybe we can come up with a more XSLTish solution for you. I belive you might be able to solve your problem (if it's the one I think it is...) by using xsl:number and by using a proper pattern in the @count attribute.

XSLT 1.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">
    <xsl:call-template name="insertHeader"/>
  </xsl:template>

  <xsl:template name="insertHeader">
    <xsl:param name="num" select="1"/>
    <xsl:value-of select="concat('This is item number ', $num, '&#x0a;')"/>
    <xsl:if test="$num &lt; 10">
      <xsl:call-template name="insertHeader">
        <xsl:with-param name="num" select="$num + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Which will output:

This is item number 1
This is item number 2
This is item number 3
This is item number 4
This is item number 5
This is item number 6
This is item number 7
This is item number 8
This is item number 9
This is item number 10
Per T
@Per T: +1 for `xsl:number` suggestion and recursive example.
Alejandro
My intentions are that I'm trying to convert some XML to HTML and want to autonumber a set of tables in it. At the moment the numbers are hardcoded which is obviously a bit awkward if one wants to insert another.
Peter
This is probably something you could solve differently, if you could attache some sample code to your question we could give it a try atleast.
Per T
@Peter: `count(precedding::table)+1` will number each `table` in a document. If you are matching some element in the input (as example `toTable[@view='True']`) for transforming into `table`, then you could use `count(precedding::toTable[@view='True'])+1`
Alejandro
+3  A: 

In a functional language as XSLT there is no "order of computation" defined.

Therefore, trying to number the "computations" by their ordering "in time" isn't meaningful and if attempted will often produce surprizing results.

For example, nothing restricts <xsl:apply-templates> to apply templates in the same order in time as the document order of the nodes in the selected node-list. These could be done in parallel, meaning in any order.

Many XSLT processors perform lazy evaluation which means that a certain XSLT instruction will only be evaluated when it is really needed and not according to its textual order in the XSLT stylesheet. Often some instructions are not executed at all.

Sometimes the optimizer will execute a given XSLT instruction twice because it decided to discard the first result in order to optimize space utilization.

The requested numbering can be produced using recursion (generally) and continuation-passing style CPS or Monads (more specifically).

The FXSL library (both version 1 -- for XSLT 1.0 and version 2 -- for XSLT 2.0) contains templates that can be used to organize such numbering: foldl, foldr, iter, iterUntil, scanl, scanr, ..., etc.

Whenever the problem is precisely defined (which is not the current case), such numbering can be produced but be warned about the results.

Dimitre Novatchev
@Dimitre: +1 good explanation. Also it's posible to declare the number relative to context node document order with `xsl:number` or `fn:count`.
Alejandro
Thanks for the answer, which I think makes it pretty clear that what I want to do can't be made to work (ie. can't autonumber based on their position in the output if that can't be entirely determined from the input).
Peter
@Peter: YOur question was about "Counting invocations of templates". This is very different from "autonumbering based on position in the output." Actually, numbering of nodes, based on their position in the output *can* be done. You will need a two-step transformation in which the 2nd step will do the numbering. If you are interested in this, please, ask a new question.
Dimitre Novatchev
@Dimitre: Thanks, but I don't want to go back to the code and do a 2-step transformation - the feature is not critical to me and if it doesn't work via one XSL transform then I'm not going to do it.
Peter
@Peter: First, everything is determined from input source whether it's only one, multiple, inline into the stylesheet, or pseudo randomness if you take seed into the account (I have to think this twice about monoids). You are reluctant just because **you can't**. And last, two step transformations whether with extensions functions in XSLT 1.0 or just with XSLT 2.0, are a very commond pattern to improve performance in some algorithms, something that you definitely want to have into your bag...
Alejandro
@Peter, I completely agree with @Alejandro 's comment. Multiple pass transformations (or functional composition) is a fundamental design pattern in XSLT and in any functional programming language.
Dimitre Novatchev
@Alejandro/Dimitre: I am not saying that 2-step transformations aren't useful. I'm saying that our current setup doesn't support them, so yes, I am reluctant because I can't do one without modifying the code which invokes the XSL transform. It's not that I couldn't do that, but I consider it more work than this feature justifies. This was a small feature which I was hoping I could sneak in by only modifying the XSLT - if I can't achieve it under that constraint, that's fine.
Peter
@Dimitre: Excelent sentence matching conceptualy multiple pass transformation with fuctional composition!
Alejandro
A: 

The numberOfInvocations must be computed outside the template and given as a parameter. Inside a for-each you can retrieve the iteration number using position().

In the following stylesheet, I added a pos parameter to insertHeader.

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:s="uri:sample">
  <xsl:output type="text"/>

  <s:sample>
    <table>Table A</table>
    <table>Table B</table>
  </s:sample>

  <xsl:template match="/">
    <xsl:apply-templates select="document('')//s:sample"/>
  </xsl:template>

  <xsl:template match="s:sample">
    <xsl:call-template name="insertHeader">
      <xsl:with-param name="pos" select="1"/>
    </xsl:call-template>

    <xsl:variable name="node-set" select="table"/>
    <xsl:variable name="node-set-count" select="count($node-set)"/>
    <xsl:for-each select="$node-set">
      <xsl:call-template name="insertHeader">
        <xsl:with-param name="pos" select="1+position()"/>
      </xsl:call-template>
      <xsl:value-of select="."/>
      <xsl:text>
</xsl:text>
    </xsl:for-each>

    <xsl:call-template name="insertHeader">
      <xsl:with-param name="pos" select="2+$node-set-count"/>
    </xsl:call-template>

  </xsl:template>

  <xsl:template name="insertHeader">
    <xsl:param name="pos"/>
    <xsl:text>This is item number </xsl:text><xsl:value-of select="$pos" />
    <xsl:text>
</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Note: the sample document is embeded inside the stylesheet, so apply the stylesheet to itself to view the result.

$ xsltproc 3663349.xslt 3663349.xslt
This is item number 1
This is item number 2
Table A
This is item number 3
Table B
This is item number 4
dolmen