xsl-list
[Top] [All Lists]

Re: [xsl] Testing if all child nodes match (based on the value of certain elements)

2007-04-23 13:55:28
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've puzzled over this for some hours with no luck.

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!


--
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--