Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT default variable value if value not present

Tags:

xml

xslt

I am trying to declare a variable that has a default value or if a value is present in a repeating set to use a new different value.

This is what I have so far.

      <xsl:variable name="lsind">
        <xsl:value-of select="'N'"/>

        <xsl:for-each select='./Plan/InvestmentStrategy/FundSplit'>
          <xsl:choose>
            <xsl:when test="contains(./@FundName, 'Lifestyle')">
              <xsl:value-of select="'Y'"/>
            </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </xsl:variable>

What I want is if any instances of ./Plan/InvestmentStrategy/FundSplit/@FundName 'contains' LifeStyle then lsind ' Y' otherwise it falls back to the default value of 'N'.

I am doing it this way as if i use 'otherwise the last occurrence could potentially set lsind back to N?

Any suggestions?

like image 798
Jon H Avatar asked Nov 14 '11 11:11

Jon H


1 Answers

<xsl:variable name="lsind">
  <xsl:choose>
    <xsl:when test="Plan/InvestmentStrategy/FundSplit[contains(@FundName, 'Lifestyle')]">
       <xsl:text>Y</xsl:text>
    </xsl:when>
    <xsl:otherwise>
       <xsl:text>N</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

should suffice

like image 181
Martin Honnen Avatar answered Sep 28 '22 12:09

Martin Honnen