xsl-list
[Top] [All Lists]

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

2007-04-23 14:04:12
Mark Anderson wrote:
Hi All

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:

<group>
        <person>
                <name>John</name>
                <age>42</age>
                <sex>M</sex>
                marital_status>S<marital_status>
<snip />

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').
The problem is usually in the '=' operator (thinking positive): in XLST, if one item in the left side sequence and one item in the right side sequence equal, the whole expression returns true. The trick is to reverse it: with the '!=' (think negative: think what you don't want and then reverse it), which means: if one item on the left side is sequence is unequal to any item in the right side sequence, the whole expression returns false:

(: returns false precisely when you want true() to return :)
group/person/sex !=group/person/sex

(: reverse the result :)
not(group/person/sex !=group/person/sex)

and *and* it together with the rest:

(: full expression :)
not(group/person/sex !=group/person/sex) and
not(group/person/age !=group/person/age)

Cheers,
-- Abel Braaksma

--~------------------------------------------------------------------
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>
--~--