Agozer Posted August 24, 2009 Share Posted August 24, 2009 So yeah, I having trouble with an XML --> XHTML transformation using XSL. More specifically I want to count the number of nodes with specific data and then output the result. My XML structure is something like this: <people> <person> <name>Mary</name> <sex>Yes please</sex> <note>bring condoms, bring wine, wears expensive lingerie</note> </person> <person> <name>Felli</name> <sex>Yes please</sex> <note>loli with a twist, mean eye</note> </person> <person> <name>Helga</name> <sex>Hell no</sex> <note>Huge girth, needs eyebleach</note> </person> <person> <name>Jenna</name> <sex>Yes please</sex> <note>bring wine, pornstar, blonde</note> </person> </people> ... etc. etc. What I'm trying to do is to count (using XSLT count() function or any other suitable XSLT method) the number of persons with both the sex element containing "yes please" as well as the note element containing the words "bring wine". I have traversed the Intarwebs for months, steering asway from porn, searching high and low, and have found lots of would-be examples, just not the one I'm looking for. All my past efforts have produced incorrect results or a result of 0, so there's something I'm doing wrong - I just can't figure out what. Link to comment Share on other sites More sharing options...
LoRd_SnOw Posted September 10, 2009 Share Posted September 10, 2009 (edited) My XML Knowledge is pretty poor, but maybe this can help, which is an example i made about a year ago. save your .xml file to data.xml <site> <post> <text>.</text> <categories> <category>Food</category> <category>Toaster</category> <category>Piano</category> </categories> </post> <post> <text>.</text> <categories> <category>Toaster</category> <category>Piano</category> </categories> </post> <post> <text>.</text> <categories> <category>Piano</category> </categories> </post> <post> <text>.</text> <categories> <category>Food</category> <category>Piano</category> </categories> </post> </site>write your xslt, then name it as transform.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each select="//category"> <xsl:variable name="value" select="."/> <xsl:if test="count(preceding::category[.=$value]) = 0"> <xsl:value-of select="."/> <xsl:text> (</xsl:text> <xsl:value-of select="count(//category[.=$value])"/> <xsl:text>)</xsl:text><br/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet> when you open in IE the following results will be categories listed, for example: Food(2) Toaster(3) Piano(4) Edited September 10, 2009 by Snow Patr0l Link to comment Share on other sites More sharing options...
Agozer Posted September 10, 2009 Author Share Posted September 10, 2009 Not what I was looking for, but a good example of the use of for-each that I can use in my future projects. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now