Click to See Complete Forum and Search --> : Xpath


bogocles
07-15-2008, 04:48 PM
Consider the following XML:



<reports>
<report created="Yesterday" file="REPORT_1.pdf">
<recipient><![CDATA[somedude@somewhere.com]]></recipient>
<recipient><![CDATA[anotherdude@somewhere-else.com]]></recipient>
</report>
<report created="Yesterday" sent="today" file="REPORT_1.pdf">
<recipient><![CDATA[somedude@somewhere.com]]></recipient>
<recipient><![CDATA[anotherdude@somewhere-else.com]]></recipient>
</report>
</reports>

I'm trying to use XPATH in an application to select all the report elements in the document based on whether or not they've been sent. I figured the following two XPATH statements would work, but so far it doesn't seem like they are:



//report[@sent]

I figured this particular XPATH statement would select all <report> elements where a sent attribute existed. Likewise, I expected ...



//report[!@sent]

... to do just the opposite. Now the first one makes sense to me, but the second one looks ugly. I've looked up and down on the web and found nothing for selecting nodes based on the existence of child nodes, only the actual values of any child nodes. Beyond that, I've found nothing that says you cannot select nodes based on child node existence.

I know XSL allows this, because I've used the following statement plenty of times:



<xsl:if select="@some-node">I'm true if some-node is an attribute of the context node</xsl:if>

But does this kind of thing work in XPATH? If it does, please oh please, point me to a url that shows this. (w3schools let me down on this one ... :( )

bogocles
07-15-2008, 08:14 PM
RESOLVED.

I found out how to do this. For anyone interested, a way to return a node list based on the existence of one or more attributes would be (for the above example):



//REPORT[count(attribute::sent) > 0] // To select nodes with sent attributes
//REPORT[count(attribute::sent) < 1] // To select nodes without sent attributes