Barlow1984
Barlow1984

Reputation: 205

XSLT 1.0: if statement when position matches a variable value

I'm trying to insert an IF statement into an existing FOR-EACH loop to do something slightly different if it matches a variable from another node (the node I want is actually a sibling of it's parent - if that makes sense!?).

The value is a simple integer. I basically want to say: If the position is equal to the variable number then do XXXX.

Here is the XSLT, it's only v1.0 and not 2.0 that I can use.

<xsl:for-each select="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='accordion_tab']/accordion_tab/sections">
<h3 class="accordionButton">
<xsl:if test="position()='openpane value to go here'">
    <xsl:attribute name="class">
    <xsl:text>new text</xsl:text>
    </xsl:attribute>
</xsl:if>
</xsl:for-each>

My XML extract is here:

<sections>
<title>title</title>
<text>some text</text>
</sections>
<openpane>2</openpane>

Upvotes: 0

Views: 1521

Answers (1)

nwellnhof
nwellnhof

Reputation: 33638

You didn't make this clear in your question, but I assume you iterate over the sections elements in your for-each loop. From the for-each loop you can reach the openpane element by going through the parent of the current sections element:

<xsl:for-each select="sections">
    <xsl:if test="position() = ../openpane">
        ...
    </xsl:if>
</xsl:for-each>

You could also define a variable referring to the openpane element first:

<xsl:variable name="openpane" select="openpane"/>
<xsl:for-each select="sections">
    <xsl:if test="position() = $openpane">
        ...
    </xsl:if>
</xsl:for-each>

Upvotes: 1

Related Questions