I have the following XML, I am trying to get the unique nodes based on the name child node.
Original XML:
<products>
<product>
<name>White Socks</name>
<price>2.00</price>
</product>
<product>
<name>White Socks/name>
<price>2.00</price>
</product>
<product>
<name>Blue Socks</name>
<price>3.00</price>
</product>
</products>
What I’m trying to get:
<products>
<product>
<name>White Socks</name>
<price>2.00</price>
</product>
<product>
<name>Blue Socks</name>
<price>3.00</price>
</product>
</products>
I’ve tried various things but not worth listing here, the closest I got was using XPath but that just returned the names like below. However, this is wrong as I want the full XML as above, not just the node values.
White Socks
Blue Socks
I’m using Ruby and trying to iterate over the nodes like so:
@doc.xpath("//product").each do |node|
Obviously the above currently gets ALL product nodes, whereas I want all unique product nodes (using the child node “name” as the unique identifier)
This transformation:
when applied on the provided XML document (corrected to be well-formed):
produces the wanted, correct result:
Do note:
The identity rule copies every node “as-is”.
The Muenchian method for grouping is used.
There is a single overriding template that excludes any
productelement that is not the first in its group.XPath-one-liner (Note this is O(N^2) — will be very slow on many
productelements):