user1961425
user1961425

Reputation: 21

Keep Duplicates in XPath Query Result

Consider the following XML example:

<books category="cat1">
   <book id="1" ... />
   <book id="2" ... />
   <book id="3" ... />
</books>

If I use the XPath query /books/book/parent::*/@category I am expected to receive one row only:

cat1

However, how can the XPath query be modified to return

cat1
cat1
cat1

(meaning that I want to keep all duplicates).

Upvotes: 2

Views: 566

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

The "/" operator in XPath automatically removes duplicates.

In XPath 1.0 there's a fundamental difficulty with your requirement: the only composite value supported in the data model is the node-set, and node-sets (being sets) cannot contain duplicates. So there's no such thing as a value containing duplicate nodes, and therefore no expression that will produce such a result.

Upvotes: 4

Mads Hansen
Mads Hansen

Reputation: 66724

In XPath 2.0 you could use a for loop:

for  $book in /books/book return $book/parent::*/@category 

Upvotes: 4

Related Questions