XSLT is XML itself. It must obey the syntax rules of XML. <
and >
are valid operators in XPath, but if you use them in XML then they must be XML-escaped.
<xsl:if test="$foo > 1"> <!-- translates to '$foo > 1' during parsing of the XSLT document -->
It seems you want to decide what to do based on the number of <PARENT_TAG>
elements. So let's count them.
<xsl:for-each select="HIGHLEVEL_TAG">
<xsl:if test="count(PARENT_TAG) = 1">
do something
</xsl:if>
<xsl:if test="count(PARENT_TAG) > 1">
do something different
</xsl:if>
</xsl:for-each>
Now if you really want to do one thing when there is only one <PARENT_TAG>
and something different when there is more than one, then this won't work. There's always at least one, which means the first <xsl:if>
will always run. There are several ways to solve this:
You could modify the first <xsl:if>
condition to explicitly exclude the case where there is more than one <PARENT_TAG>
.
Better would be an <xsl:choose>
instead of an <xsl:if>
- make sure you test for the "more than one" case first.
<xsl:for-each select="HIGHLEVEL_TAG">
<xsl:choose>
<xsl:if test="count(PARENT_TAG) > 1">
do something different
</xsl:if>
<xsl:if test="count(PARENT_TAG) = 1">
do something
</xsl:if>
</xsl:choose>
</xsl:for-each>
But actually the idiomatic way of dealing with this is to use templates - one for the specific case (there is more than one <PARENT_TAG>
), and a generic one that matches all the other cases (0 or 1 <PARENT_TAG>
):
<xsl:template match="HIGHLEVEL_TAG[count(PARENT_TAG) > 1]">
do something different
</xsl:template>
<xsl:template match="HIGHLEVEL_TAG">
do something
</xsl:template>
Now you can drop the <xsl:for-each>
and call:
<xsl:apply-templates select="HIGHLEVEL_TAG" />
and the XSLT processor will sort it out for you.
If you are unsure how <xsl:apply-templates>
works, take a look at What are the differences between 'call-template' and 'apply-templates' in XSL?.
The same approach also works for the "I want to do something else for all occurrences of <PARENT_TAG>
greater than one" case, the only difference is that we use position()
instead of count()
to decide which template should run:
<xsl:template match="PARENT_TAG[position() > 1]">
do something different
</xsl:template>
<xsl:template match="PARENT_TAG">
do something
</xsl:template>
and
<xsl:apply-templates select="PARENT_TAG" />