1
votes

My XML is completely recursive in that every element within it is an "Item" and the type is delineated by a "type" property. In my XSL I want to be able to determine what level of iteration I am at, or in other words, how many levels from the root I am currently. I can't figure out how to do this . . .

XML Sample:

<Questionnaire>
    <Item ItemType="Group">
        <Caption>ABC</Caption>
        <Item ItemType="Group">
            <Caption>DEF</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
    </Item>
    <Item ItemType="Group">
        <Caption>QRS</Caption>
        <Item ItemType="Group">
            <Caption>TUV</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
        <Item ItemType="Group">
            <Caption>XYZ</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
    </Item>
</Questionnaire>

XSL Sample:

<xsl:template match="/Questionnaire">
    <xsl:for-each select="Item">
        <fieldset>
            <legend><xsl:value-of select="Caption" /></legend>
            <xsl:call-template name="ItemTemplate" />
        </fieldset>         
    </xsl:for-each>
</xsl:template>

<xsl:template name="ItemTemplate">
    <xsl:if test="@ItemType != 'Question'">
        <ol>
            <xsl:for-each select="Item">
                <li>
                    <xsl:value-of select="Caption" />
                    <xsl:call-template name="ItemTemplate" />
                </li>
            </xsl:for-each>
        </ol>
    </xsl:if>
</xsl:template>
3

3 Answers

4
votes

This?

count(ancestor::Item)

By the way, it is more idiomatic in XSLT to use <xsl:template match=...> and <xsl:apply-templates>, rather than named templates and <xsl:call-template>.

0
votes
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/Questionnaire">
        <root>
            <xsl:for-each select="Item">
                <fieldset>
                    <legend>
                        <xsl:value-of select="Caption"/>
                    </legend>
                    <xsl:call-template name="ItemTemplate"/>
                </fieldset>
            </xsl:for-each>
        </root>
    </xsl:template>
    <xsl:template name="ItemTemplate">
        <xsl:variable name="Count">
            <xsl:call-template name="count">
                <xsl:with-param name="Node" select="."/>
                <xsl:with-param name="PrevCount" select="1"/>
            </xsl:call-template>
        </xsl:variable>
         (This is <xsl:value-of select="$Count"/> deep)
        <xsl:if test="@ItemType != 'Question'">
            <ol>
                <xsl:for-each select="Item">
                    <li>
                        <xsl:value-of select="Caption"/>
                        <xsl:call-template name="ItemTemplate"/>
                    </li>
                </xsl:for-each>
            </ol>
        </xsl:if>
    </xsl:template>
    <xsl:template name="count">
        <xsl:param name="Node"/>
        <xsl:param name="PrevCount"/>
        <xsl:choose>
            <xsl:when test="name($Node)=name($Node/..)">
                <xsl:call-template name="count">
                    <xsl:with-param name="Node" select="$Node/.."/>
                    <xsl:with-param name="PrevCount" select="$PrevCount+1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$PrevCount"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

The third template does the counting by walking the tree recursively. This extract from above gets the count in a variable:

    <xsl:variable name="Count">
        <xsl:call-template name="count">
            <xsl:with-param name="Node" select="."/>
            <xsl:with-param name="PrevCount" select="1"/>
        </xsl:call-template>
    </xsl:variable>

And this displays it:

     (This is <xsl:value-of select="$Count"/> deep)

It's not difficult once you realise how to count by doing recursive loops. :-)

0
votes

The simplest solution I can think of is to define a param depth in your ItemTemplate template, and then call it with-param of 0 (initally) and $depth+1 subsequently.

Below are my modifications:

  <xsl:template match="/Questionnaire">
    <xsl:for-each select="Item">
      <fieldset>
        <legend><xsl:value-of select="Caption" /></legend>
        <xsl:call-template name="ItemTemplate">
          <xsl:with-param name="depth" select="1" />
        </xsl:call-template>
      </fieldset>                     
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="ItemTemplate">
    <xsl:param name="depth" />
    <xsl:if test="@ItemType != 'Question'">
      <ol>
        <xsl:for-each select="Item">
          <li>
            <xsl:value-of select="Caption" />
            <xsl:value-of select="$depth" />
            <xsl:call-template name="ItemTemplate">
              <xsl:with-param name="depth" select="1+$depth" />
            </xsl:call-template>
          </li>
        </xsl:for-each>
      </ol>
    </xsl:if>
  </xsl:template>