开发者

How to check if a node's value in one XML is present in another XML with a specific attribute?

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 开发者_如何学Gohow to break in between (and set a variable) if a show="false" is found.

Is it possible? Any help/comments appreciated.


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


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.


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜