How To Compare Node To A String In XSLT



TODO:

Have you ever wanted to compare a node value to a string and use a new value when there is a match?

 

SOLUTION:

 

<xsl:variable name="newValue">
	<xsl:choose>
		<xsl:when test="string($myRoot/MyNode) = 'Some Value'">123456</xsl:when>
              	<xsl:otherwise>
                	<xsl:value-of select="string($myRoot/MyNode)"/>
              	</xsl:otherwise>
	</xsl:choose>
</xsl:variable>          

<MyDataNode>
	<xsl:value-of select="$newValue"/>
</MyDataNode>

 

NOTES:

The above checks the value of $myRoot.MyNode and assigns a value if there is a match.  If there is no match, the "otherwise" is reached and I use the value in the node.  I then use that value for MyDataNode.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading