Reputation: 167
Is there a way to count how many strings in other nodes are contained in a different text node using XSL (ver=1.0)? I have the following xml and need to count how many colors appear in a given text. The output should be:
Color counts: 2 (white, gold)
Color counts: 2 (red, yellow)
The xml looks like:
<items>
<colors>
<color>red</color>
<color>yellow</color>
<color>blue</color>
<color>white</color>
<color>purple</color>
<color>gold</color>
<color>silver</color>
</colors>
<item>
<text>
Mr. Johnson prefers a white with gold logo truck.
</text>
<text>
Mr. Johnson prefers a red with logo yellow truck.
</text>
</item>
</items>
Upvotes: 0
Views: 70
Reputation: 117083
Try it along these lines:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/items">
<xsl:for-each select="item/text">
<xsl:variable name="colors" select="../../colors/color[contains(current(), .)]" />
<xsl:text>Color counts: </xsl:text>
<xsl:value-of select="count($colors)"/>
<xsl:text> (</xsl:text>
<xsl:for-each select="$colors">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>) </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1