Record conditional records in XSLT

I have xml with this structure:

<emails> <record> <field name="host"><![CDATA[yahoo]]></field> <field name="user"><![CDATA[abc]]></field> </record> <record> <field name="host"><![CDATA[gmail]]></field> <field name="user"><![CDATA[abc]]></field> </record> <record> <field name="host"><![CDATA[yahoo]]></field> <field name="user"><![CDATA[cdx]]></field> </record> </emails> 

And, I want to count the number of entries where host = yahoo. I know that I need to use count (), but I could not figure out how to do this.

+4
source share
1 answer

Assuming you were put in an email element, this is an expression that you probably want

 <xsl:value-of select="count(record[field[@name='host']/text()='yahoo'])" /> 

For example, try this XSLT

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/emails"> <xsl:value-of select="count(record[field[@name='host']/text()='yahoo'])" /> </xsl:template> </xsl:stylesheet> 

Assuming your XML was well-formed and your CDATA tags were formatted correctly, it should output 3.

+8
source

All Articles