Write XSLT conditions based on number of occurrence of some element in XML
Sometime we have complex scenarios which need to handle in XSLT. One simple use case is writting conditional tags in output based on some values of input XML.
To handle this type of scenario, simple way is to declare variable & use it in XSLT wherever needed. Below is the way to declare variable in XSLT-
<xsl:variable name="varCount" select="count(ParentElement/ChildElement)" />
In my scenario i need to write some condition based on number of elements "ParentElement/ChileElement" received in input XML file.
Now we can write conditional IF statement in XSLT as below-
<xsl:if test="$varCount = 0">
<Result>No element found</Result>
</xsl:if>
<xsl:if test="$varCount > 0">
<Result>element found</Result>
</xsl:if>
To handle this type of scenario, simple way is to declare variable & use it in XSLT wherever needed. Below is the way to declare variable in XSLT-
<xsl:variable name="varCount" select="count(ParentElement/ChildElement)" />
In my scenario i need to write some condition based on number of elements "ParentElement/ChileElement" received in input XML file.
Now we can write conditional IF statement in XSLT as below-
<xsl:if test="$varCount = 0">
<Result>No element found</Result>
</xsl:if>
<xsl:if test="$varCount > 0">
<Result>element found</Result>
</xsl:if>
Comments
Post a Comment