first time poster and XPath beginner. I hope you can help me with this XPath problem I have. Here's a simplified example of my file.
<town>
<library>
<section>
<book>
<title>ABC
</title>
</book>
<book>
<title>DEF
</title>
</book>
</section>
<section>
<book>
<title>ABC
</title>
</book>
<book>
<title>DEF
</title>
</book>
</section>
</library>
<library>
<section>
<book>
<title>ABC
</title>
</book>
<book>
<title>DEF
</title>
</book>
</section>
<section>
<book>
<title>ABC
</title>
</book>
<book>
<title>DEF
</title>
</book>
<book>
<title>GHI
</title>
</book>
</section>
</library>
</town>
What I'm trying to obtain with an XPath is the "book" elements that have a unique "title", but unique within each of the two "library" elements, not unique within the root "town" node.
So, based on my example above, I expect to get the first "ABC" and the first "DEF" of the first "library", and the first "ABC", the first "DEF" and the first "GHI" of the second "library".
distinct-values(//library[1]//book | //library[2]//book)
This works but I cannot use it because the CMS in which I need to use the XPath doesn't recognise that function...
//library//book[not(./title = preceding::*/title)]
This does not select the first "ABC" and the first "DEF" in the second "library", and I don't understand why. Is there a way to limit the scope of the "preceding" axis so it stops at its own "library" element?
If someone has understood what I'm trying to achieve, I'll be really grateful for any help!
It looks like the CMS you are using only supports XPath 1.0. Start by lobbying them to change that - XPath 1.0 was superseded by 2.0 in 2007 and should be ancient history by now.
This is very hard (perhaps impossible) to do in a single XPath 1.0 expression. There are solutions in XSLT 1.0 (search for Muenchian grouping) but not, I think, in pure XPath 1.0.