[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Testing if all child nodes match (based on the val
At 2007-04-23 21:21 +0100, Mark Anderson wrote:
I'm stuck with trying to figure something out in an XSL style sheet. I can't post may actual data, as it's confidential, so I've made up an example: ... What I want to do is find out if the group contains all people of the same sex and age (in this case it does). So in my example xml structure, for every person in the group <age> and <sex> must be the same (but I've no idea what their values will be at run time: i.e. I can't check if age is '42' and sex = 'M'). I have an example of this in my XSLT class because it is not intuitive. It is on page 255 of the twelfth edition of the PDF book version of the material for those who want to follow up. Because a node set test only repeats when the test result is false, the way to check that something is true for all members is to check that it is not all false for all members. An example of this inverted logic is below with your test data, with a second test file that has a discrepancy. I hope this helps. . . . . . . . . . . . . . . Ken t:\ftemp>type anderson.xml <group> <person> <name>John</name> <age>42</age> <sex>M</sex> <marital_status>S</marital_status> </person> <person> <name>Fred</name> <age>42</age> <sex>M</sex> <marital_status>W</marital_status> </person> <person> <name>Harry</name> <age>42</age> <sex>M</sex> <marital_status>M</marital_status> </person> <person> <name>Brian</name> <age>42</age> <sex>M</sex> <marital_status>S</marital_status> </person> </group> t:\ftemp>type anderson2.xml <group> <person> <name>John</name> <age>42</age> <sex>M</sex> <marital_status>S</marital_status> </person> <person> <name>Fred</name> <age>42</age> <sex>M</sex> <marital_status>W</marital_status> </person> <person> <name>Harry</name> <age>42</age> <sex>M</sex> <marital_status>M</marital_status> </person> <person> <name>Brian</name> <age>41</age> <sex>M</sex> <marital_status>S</marital_status> </person> </group> t:\ftemp>call xslt anderson.xml anderson.xsl report.txt t:\ftemp>type report.txt All the same! t:\ftemp>call xslt anderson2.xml anderson.xsl report.txt t:\ftemp>type report.txt Not all the same! t:\ftemp>type anderson.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:choose> <xsl:when test="not( group/person/age != group/person/age ) and not( group/person/sex != group/person/sex )"> <xsl:text>All the same!</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>Not all the same!</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> t:\ftemp>rem Done!
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|