user1620642
user1620642

Reputation: 79

Testing conditions in XSLT with xsl:when

I have the below xsl tag in which i am fetching out the values of fpml:periodMultiplier and fpml:period as shown below ... tags in xml are :-

<fpml:periodMultiplier>1</fpml:periodMultiplier>
<fpml:period>Y</fpml:period>

extracting in xsl as shown below

<Payindextenor>
<xsl:value-of select="../fpml:calculationPeriodDates
                        /fpml:calculationPeriodFrequency
                        /fpml:periodMultiplier" />
<xsl:value-of select="../fpml:calculationPeriodDates
                        /fpml:calculationPeriodFrequency
                        /fpml:period" />
</Payindextenor>

so the value of Payindextenor is 1Y

now i want to put the null check in this tag since it may happen that in coming xml there can be no value for fpml:periodMultiplier and fpml:period also.

so i have come up with below xsl implementation in which i have tried that if any of the value is null then it should print null pls advise is it correct one :-

<xsl:choose>
  <xsl:when test="../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:periodMultiplier
                  != ' ' 
                  and 
                  ../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:period 
                  != ' '">
    <xsl:value-of select="../fpml:calculationPeriodDates
                             /fpml:calculationPeriodFrequency
                            /fpml:periodMultiplier" />
   <xsl:value-of select="../fpml:calculationPeriodDates
                           /fpml:calculationPeriodFrequency
                           /fpml:period" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="'null'" />
  </xsl:otherwise>
</xsl:choose>

Upvotes: 0

Views: 201

Answers (2)

JLRishe
JLRishe

Reputation: 101652

As Ian Roberts has said, comparing a node to a single space is very different from checking for "null", but assuming you want to display "null" when both periodMultiplier and period are blank, you can do this:

<xsl:variable name="freq" 
         select="../fpml:calculationPeriodDates/fpml:calculationPeriodFrequency" />
<xsl:choose>
  <xsl:when test="$freq/fpml:periodMultiplier != '' or 
                  $freq/fpml:period != ''">
    <xsl:value-of select="$freq/fpml:periodMultiplier" />
    <xsl:value-of select="$freq/fpml:period" />
  </xsl:when>
  <xsl:otherwise>
      <xsl:text>null</xsl:text>
  </xsl:otherwise>
</xsl:choose>

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122364

This is exactly the same situation as in your previous question - you're comparing against the (non-empty) string ' ' containing a single space, when what you actually want is to check for empty strings. And you can use the same solution as I suggested for that question, and test using normalize-space (which treats empty strings and strings containing only whitespace as "false" and anything else as "true"):

<xsl:choose>
  <xsl:when test="normalize-space(../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:periodMultiplier)
                  and 
                  normalize-space(../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:period)">
    <xsl:value-of select="../fpml:calculationPeriodDates
                             /fpml:calculationPeriodFrequency
                            /fpml:periodMultiplier" />
   <xsl:value-of select="../fpml:calculationPeriodDates
                           /fpml:calculationPeriodFrequency
                           /fpml:period" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="'null'" />
  </xsl:otherwise>
</xsl:choose>

This will handle cases both where fpml:periodMultiplier or fpml:period elements are absent, and where they're present but empty.

Upvotes: 2

Related Questions