1

I need to make some kind of identifier/flag when reaching some if statements. So that I only write the <h2> once in the for-each (Code is below):

<xsl:for-each select="$matchedNodes">
    <xsl:variable name="i" select="0"/>
    <xsl:variable name="j" select="0"/>
    <xsl:if test="magazineDate != '' and (i &lt; 1)">
        <h2>Magasiner</h2>
        i++
    </xsl:if>

    <xsl:if test="homepageArticleDate != '' and (j &lt; 1)">
        <h2>Artikel</h2>
        j++
    </xsl:if>
</xsl:for-each>

I've tried using position() but this won't work since both properties are within $matchedNodes

$matchedNodes consists of umbraco nodes.

Can anyone see a solution for this problem? I've thought about using xsl:template but im a bit of a newbie at XSLT so I didnt know where to start.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Anonymous
  • 1,303
  • 4
  • 19
  • 31
  • Using boolean variable as a flag is allowed to – Anonymous Sep 25 '15 at 11:20
  • 1
    Consider to show us a sample of the matchedNodes input nodes and the corresponding output (HTML `h2` elements?) you want to create, then we can show you an XSLT way of achieving that. – Martin Honnen Sep 25 '15 at 11:25
  • matchedNodes consists of umbraco nodes – Anonymous Sep 25 '15 at 12:24
  • Variables are "immutable" in XSLT and cannot be changed once set, and so your current approach is not going to work. To solve your problem, we really need to see the input XML, and the expected output HTML, and then another approach can be given. Thank you! – Tim C Sep 25 '15 at 14:19

1 Answers1

0

XSLT and XPath are functional languages. This, among other things, means that variables cannot be changed, once their value is defined. Certainly, there are alternative solutions, that do not need to mutate any variable -- in many ways immutability is a superior language feature, leading to more understandable and maintainable code and which allows for more aggressive optimization than in the case of OIL (Old Imperative Languages).

One solution could look like this:

    <xsl:apply-templates select=
    "$matchedNodes/magazineDate[normalize-space()][1]
  |  $matchedNodes/homepageArticleDate[normalize-space()][1]" mode="heading"/>

And then in two different/separate templates (one could even try to unify them into a single template):

<xsl:template match="magazineDate" mode="heading">
  <h2>Magasiner</h2>
</xsl:template>

<xsl:template match="homepageArticleDate" mode="heading">
  <h2>Artikel</h2>
</xsl:template>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431