tags:

views:

52

answers:

4

The question seems to be little confusing. So let me describe my situation through an example:

Suppose I have an XML: A.xml

<Cakes>
    <Cake>Egg</Cake>
    <Cake>Banana</Cake>
</Cakes>

Another XML: B.xml

<Cakes>
    <Cake show="true">Egg</Cake>
    <Cake show="true">Strawberry</Cake>
    <Cake show="false">Banana</Cake>
</Cakes>

Now I want to show some text say "TRUE" if all the Cake in A.xml have show="true" in B.xml else "FALSE".

In the above case, it will print FALSE.

I need to develop an XSL for that. I can loop through all the Cake in A.xml and check if that cake has show="true" in B.xml but I don't know how to break in between (and set a variable) if a show="false" is found.

Is it possible? Any help/comments appreciated.

+1  A: 

The problem here is that you can't update Variables in XSL; XSL Variables are Assign Once only, but that can be assigned conditionally.

I think the simpler and saner option would be to use smaller fragments of XSL and programatically call them from a proper programming language, which can use XPath to make descisions.

Russ C
+1  A: 
<xsl:choose>
<xsl:when test="count(document('B.xml')/Cakes/Cake[@show='true' AND text() = /Cakes/Cake/text()]) = count(/Cakes/Cake)">
true
</xsl:when>
<xsl:otherwise>
false
</xsl:otherwise>

Should help you (not 100% sure)

remi bourgarel
Great...worked with some tweaks.... Thanks a lot.... :)
Manish
+1  A: 

I think the best way of putting it would be:

Is there at least one element "Cake" into "A.xml" that not have the same string-value than any element "Cake" into "B.xml" whose attribute "show" is "true" ?

So, in xpath 1.0:

document('A.xml')/Cakes/Cake[not(. = document('B.xml')/Cakes/Cake[@show='true'])]

Note: I apologize for the translation. I speak Spanish.

Alejandro
I admire the work of Dimitre, but I think his answer is wrong. It is translated as the negation of the following condition: any element "Cake" with attribute "show" not equal to "true" into "B.xml" file has a string-value equal to some element "Cake" into "A.xml" file (Actually only if the transformation is applied on A.xml). So, this checks if there is some Cake node on A.xml that has an explicit match with a "not show" Cake node in B.xml. But, what if there is not a Cake node in B.xml whith attribute show neither "true" or "false"?
Alejandro
+1  A: 

One of the simplest XPath expressions that correctly evaluates this condition is:

not(document('B.xml')/*/Cake[@show !='true'] = /*/Cake)
Dimitre Novatchev