XML XPath help

Status
Not open for further replies.

Bamboozled

Ars Tribunus Angusticlavius
6,303
Subscriptor
Hello,

I tried to figure this out myself and then I went to the Net but I am still stuck. Your assistance would be appreciated. Given the following XML content
Code:
<Data>
	<ItemSet Id="aList">
		<Row Id="1">
			<Item Id="Transport">Plane</Item>
			<Item Id="Type">Jet</Item>
			<Item Id="Locale">Local</Item>
			<Item Id="Rating">5</Item>
		</Row>
		<Row Id="2">
			<Item Id="Transport">Train</Item>
			<Item Id="Type">Diesel</Item>
			<Item Id="Locale">North</Item>
			<Item Id="Rating">9</Item>
		</Row>
		<Row Id="3">
			<Item Id="Transport">Train</Item>
			<Item Id="Type">Coal</Item>
			<Item Id="Locale">North</Item>
			<Item Id="Rating">12</Item>
		</Row>
		<Row Id="3">
			<Item Id="Transport">Train</Item>
			<Item Id="Type">Electricity</Item>
			<Item Id="Locale">South</Item>
			<Item Id="Rating">10</Item>
		</Row>
	</ItemSet>
</Data>

I would like to create an XPath statement that will return, for example, where "Transport" is equal to train and "locale" is equal to North. My best guess, so far, is
Code:
//Item[@Id="Transport"][.="Train" and Item[@Id="Locale"][.="North"]]
but aside from the fact that it returns no matches, I get the sense that the second clause is incorrect in that it really does not equate to "and of the nodes that match the first criteria should also match the second".
Code:
//Item[@Id="Transport"][.="Train" and //Item[@Id="Locale"][.="North"]]
This attempt returned all nodes that match "Train" but did not filter out the non-"North" locales.

What am I doing wrong?

Thanks for reading and Happy New Year.
 

Caelestian

Ars Centurion
236
Subscriptor++
Bamboozled":oqj14h7e said:
Code:
//Item[@Id="Transport"][.="Train" and Item[@Id="Locale"][.="North"]]

This says find me a Item element whose id attribute is "Transport" with a text value of "Train" and has a child node Item with an Id of "Locale" and text value is "North"

what you were probably looking for was something like this: add the following-sibling axis to your Locale Item filter (untested)
Code:
//Item[@Id="Transport"][.="Train" and following-sibling::Item[@Id="Locale"][.="North"]]


Bamboozled":oqj14h7e said:
Code:
//Item[@Id="Transport"][.="Train" and //Item[@Id="Locale"][.="North"]]

This says find me all Item elements whose id attribute is "Transport" with a text value of "Train" as long as there is an Item element in the document with an Id of "Locale" and text value is "North"
// is the the descendent axis operator from the document root, .// is the descendent operator relative to your current context
 

Alamout

Ars Legatus Legionis
27,285
I don't know if you can nest things like that, but you should be able to just list them sequentially and it's implicitly an "and". Edit: Guess I'm wrong, I never wrote it that way though.

I assume you want the Row nodes, not the Items? Your second query looks like it will match the "Train" Items, but this seems dubiously useful to me.

If you want to get the "Train" Items, you could use following-sibling to match the Locale item (this relies on the nodes being in the correct order, though):
Code:
//Item[@Id="Transport"][.="Train"][following-sibling::Item[@Id="Locale"]/text() = "North"]
Or you could get the Row nodes by matching both children:

Code:
//Row[child::Item[@Id="Transport"]/text() = "Train"][child::Item[@Id="Locale"]/text() = "North"]
Written but untested! Also I might be using slightly different syntax (I always used XPath via lxml and Python).

Edit: Beaten, but I like my Row selector better :p
 

Bamboozled

Ars Tribunus Angusticlavius
6,303
Subscriptor
Thank you both.

Caelestian, thank you for the explanations. For the first, which returned no results, I learn that that syntax indicates a progression down a tree and I can see it now.

For the second, the results indicate that the first condition is met and the second is ignored. The "and" in this context seems to act like an "or"; seems I say. The results are the application of the query to real data and independently and manually confirmed.

Alamout, thank you as well. I like your Row selector as well as it lends itself to automation very easily. As you may have surmised, I am not well versed on the various operator.

Off to code and then some bubbly. Happy New Year.
 

Caelestian

Ars Centurion
236
Subscriptor++
Bamboozled":1ojuw4p8 said:
Thank you both.
or the second, the results indicate that the first condition is met and the second is ignored. The "and" in this context seems to act like an "or"; seems I say.

The issue with the second was that it was trivially true:
Code:
//Item[@Id="Locale"][.="North"]
will return true as long as there was an <Item id="Locale">North</Item> anywhere in the input document (even if it's in a different row than the current context), cause it's the document root descendant operator

Code:
/      root element
//X    all X elements in document (descendent from root)
.      current node
./X    children X of current element
..     Parent node
../X   children X of parent
 
Status
Not open for further replies.