My XML looks like this:
<foo>
<bar name="a">
<baz name="xyz">
<time>2</time>
<date>3</date>
</baz>
</bar>
<bar name="b">
<baz name="xyz">
<time>2</time>
<date>3</date>
</baz>
</bar>
<bar name="c">
<baz name="xyz">
<time>2</time>
<date>3</date>
</baz>
</bar>
</foo>
I am writing an XSL that needs to function like this: If all the baz
children are same then doSomething
else doSomethingElse
. My current node is foo
.
I am new to XSLT and I am aware of the conditionals in XSL. It looks something like this as of now:
<xsl:template match="foo">
<xsl:choose>
<xsl:when test="[My condition]">
doSomething()
</xsl:when>
<xsl:otherwise>
doSomethingElse()
</xsl:otherwise>
</xsl:choose>
</xsl:template>
In the current example, it should doSomething()
as all the baz
elements are the same.
If I find out the number of unique baz
elements, I can test whether it is equal to one. If it is, then I will doSomething()
else doSomethingElse()
How should I implement this? What should MyCondition
be?
PS: My XSL version is 1.0
This is confusing because:
baz
are not children offoo
;Try something like:
Note that this assumes that every
baz
has adate
and atime
. Otherwise you need to test fornot(date=$first-baz/date)
etc.See also: http://www.jenitennison.com/xslt/grouping/muenchian.html
Added:
This makes it significantly more complex. Still you could construct a key:
then make your test:
This returns true if any child of
baz
exists whose string-value is different from an equally named node that is child of the firstbaz
.Note that the key, as defined here, is document-wide. If you want to restrict the test to the current
foo
ancestor, then you must include its id in the key.If you want to do this in a generic way then you are really stretching the capability of XSLT 1.0 beyond its design limits. But it can be done.
Write a named template called deep-equal that takes two elements as its arguments and returns a string containing the character "F" if and only if they are not equal (for your purposes).
It might look like this:
Apply this to all relevant pairs of elements:
Test if the result contains an "F":