[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Need help OR'ing in XPATH.


Subject: Re: [xsl] Need help OR'ing in XPATH.
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Wed, 22 Mar 2006 12:39:08 +0000

You were pretty much there..
try this,, <xsl:a-t select="book[owner='aaa' or chapter/owner='aaa']"/>
not that it will happen to you but its a best practice to refer a text value of a node by text(). In case you have child nodes for owner then u will find yourself troubled.
let us assume you had a structure like
<book id="1">
<owner>aaa
<id>111</id>
</owner>
<chapter>
<owner>aaa
<id>222</id>
</owner>
</chapter>
<chapter>
<owner>ccc</owner>
</chapter>
</book>
<book id="2">
<owner>bbb</owner>
<chapter>
<owner>bbb</owner>
</chapter>
<chapter>
<owner>ccc</owner>
</chapter>
</book>


then

<xsl:a-t select="book[owner='aaa' or chapter/owner='aaa']"/>

will not select any of the nodes in the above XML. as owner will actually give you the text value of the node and the text value of all the children under the node. so you would be wanting to be safe and rewrite the statement as

<xsl:a-t select="book[owner/text()='aaa' or chapter/owner/text()='aaa']"/>

by above you are asking for the text value of the node called owner. so you would think this makes perfect sence. but since you see that the text value actually is not just 'aaa' but has some whitespaces, the above statement will also fail to cater to your needs. So you would want to be safe and rewrite the above statement as

<xsl:a-t select="book[normalize-space(owner/text())='aaa' or normalize-space(chapter/owner/text())='aaa']"/>

This will clearly tell what you want.

As I said earlier, you may not have the problem with your XML,
<xsl:a-t select="book[owner='aaa' or chapter/owner='aaa']"/>
will still work..... but as i had pointed to you It WONT work if the owner has a child node ..
be it an empty space node like
<owner>


aaa

</owner>

or a valid XML element.

like
<owner>

aaa
<id>1111</id>
</owner>

so be safe..... and specify exactly what you want the processor to do.
HTH
Vasu

On 3/22/06, Glen Mazza <grm7793@xxxxxxxxxxx> wrote:
> Hello,
>
> For my contrived example below, I would like to select all book nodes
> whose owner is "aaa" *or* which have a chapter whose owner is "aaa", and
> I would like the books to be retrieved in the same general order as the
> xml document, i.e., the query for the example below would process books
> with id's 1, 3, 4 (in that order), not 1, 4, 3.
>
> I can do the XPATH for *either* of the two cases, but don't know how to
> combine the two for an OR search in my xsl:apply-templates select statement.
>
> <xsl:a-t select="book[owner='aaa'] or book/chapter[owner='aaa']"/>
>
> is kind of what I want, but Xalan is complaining that my select
> attribute is just returning a boolean, so my syntax is apparently wrong.
>
> Any help would be much appreciated.
>
> Thanks,
> Glen
>
>
> <book id="1">
> <owner>aaa</owner>
> <chapter>
> <owner>bbb</owner>
> </chapter>
> <chapter>
> <owner>ccc</owner>
> </chapter>
> </book>
> <book id="2">
> <owner>bbb</owner>
> <chapter>
> <owner>bbb</owner>
> </chapter>
> <chapter>
> <owner>ccc</owner>
> </chapter>
> </book>
> <book id="3">
> <owner>ccc</owner>
> <chapter>
> <owner>aaa</owner>
> </chapter>
> <chapter>
> <owner>ccc</owner>
> </chapter>
> </book>
> <book id="4">
> <owner>aaa</owner>
> <chapter>
> <owner>bbb</owner>
> </chapter>
> <chapter>
> <owner>ccc</owner>
> </chapter>
> </book>



_________________________________________________________________
Are you using the latest version of MSN Messenger? Download MSN Messenger 7.5 today! http://join.msn.com/messenger/overview



Current Thread
Keywords