I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<abc1 formName="Form">
<Level1>
<Element1>ZZZ</Element1>
<Element2>
<SubElement1>Apples</SubElement1>
<SubElement2>Oranges</SubElement2>
<SubElement3>Pears</SubElement3>
<SubElement4>Blueberries</SubElement4>
<SubElement5>Milkshakes</SubElement5>
</Element2>
</Level1>
<Level1>
<Element1>XXX</Element1>
<Element2>
<SubElement1>Apples</SubElement1>
<SubElement2>Oranges</SubElement2>
<SubElement3>Kiwifruit</SubElement3>
<SubElement4>Blueberries</SubElement4>
<SubElement5>Soda</SubElement5>
</Element2>
</Level1>
</abc1>
and what I need to be able to do is take a look at the values in some nodes, determine if they are different, and if so write some different html code with a page break between each section.
So, I need to compare the <Element1> values and see if they are different. The <Element1> values could be any text, and the number of <Element1>tags could be unlimited in the xml.
So in this case, we have two different <Element1> values: ‘ZZZ’ and ‘XXX’.
So something like the following if there are two ”:
<xsl:choose>
<xsl:when test="differentvalues = 'true'">
<!-- SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!-- SomeHTMLCode!-->
</xsl:when>
<xsl:otherwise><!-- SomeHTMLCode!--></xsl:otherwise>
</xsl:choose>
but using the for-loop to search through the xml, and something like
<xsl:choose>
<xsl:when test="differentvalues = 'true'">
<!-- SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!-- SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!-- SomeHTMLCode!-->
</xsl:when>
<xsl:otherwise><!-- SomeHTMLCode!--></xsl:otherwise>
</xsl:choose>
if there are three different </Element1> values.
I’m not even sure if this can be done.
Thanks in advance for any help you can provide me with.
This XPath expression gives you the wanted count of distinct values for
Element1(supposing anElement1cannot have a descendent alsoElement1(there is more efficient way to handle such problems using keys, but I wouldn’t talk about keys to a newbie):You can define a variable specifying the above value in its
selectattribute and then use this variable in thetestattribute` of your conditional instructions.Even better, don’t use conditional instructions at all and specify the above XPath expression as part of a template match pattern:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
When the same transformation is applied on the following XML document:
again the correct result is produced:
Note:
You need, of course, to substitute the bodies of the templates with your desired processing.