Subject: RE: Navigating an XML file with xsl:for-each etc.
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 26 Aug 2005 16:44:25 +0100
|
is:
>
> I have an XML file like this:
>
> <a>
> </a>
> <b action='change'>
> <add>
> <c>bla</c>
> <d>bla</d>
> </add>
> <delete>
> <c>bla</c>
> <d>bla</d>
> <delete>
> </b>
> <e>
> </e>
>
>
> I have to check for each attribute called 'change'
> Then look if it has an <add> tag and an <delete> tag
> - Compare the sub element of <add> called <c> with the
> sub element of <delete> called <c> and see if the
> value is equal.
> - The same with sub element <d>
>
>
> I guess the best to do that is to use a for-each to check all the
> attributes with the value 'change' (This is not working in my example
> below though)
> 1) How do I use for-each with attributes? Like this?
>
> <xsl:for-each select="//[action='change']">
<xsl:for-each select="//*[@*='change']"
Note this selects the element that has an attribute whose value is 'change',
it doesn't select the attribute itself.
> <xsl:if test=".='add'">
Now you want to test if the context element has a child element called 'add'
<xsl:if test="child::add"> (or you can miss out "child::")
>
> 2) How do I compare in the most efficient way the two sub-elements <c>
> and <d> against each other?
>
It's not clear to me whether add/delete always have children called c and d,
or whether you just used these as examples. If the former,
xsl:if test="add/c = delete/c and add/d = delete/d"
A generic comparison is more difficult, and you need to specify the
requirements rather more precisely - for example, is order important?
Michael Kay
http://www.saxonica.com/
|