Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:58:10+00:00 2026-05-20T01:58:10+00:00

I have the following XML, I am trying to get the unique nodes based

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:58:11+00:00Added an answer on May 20, 2026 at 1:58 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kProdByName" match="product"
      use="name"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "product
        [not(generate-id()
            =
             generate-id(key('kProdByName',name)[1])
             )
        ]"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document (corrected to be well-formed):

    <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>
    

    produces the wanted, correct result:

    <products>
      <product>
        <name>White Socks</name>
        <price>2.00</price>
      </product>
      <product>
        <name>Blue Socks</name>
        <price>3.00</price>
      </product>
    </products>
    

    Do note:

    1. The identity rule copies every node “as-is”.

    2. The Muenchian method for grouping is used.

    3. There is a single overriding template that excludes any product element that is not the first in its group.


    XPath-one-liner (Note this is O(N^2) — will be very slow on many product elements):

     /*/product[not(name = following-sibling::product/name)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML: <XMLDictionary> <a>b</a> <c>d</c> <e>f</e> </XMLDictionary> I'm trying to get
I have the following code where i'm trying to get the c:CreationDate nodes: value
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
I have the following XML: <funds> <fund name=A ITEM0=7% ITEM1=8% ITEM2=9% ITEM3=10% ITEM4=11% ITEM5=
I have the following XML. I'm trying to make a static button underneath my
I have the following line of xml that I'm trying transform using XSLT but
I have the following XML and I have been trying Descendents().Descendents().Descendents to retrieve an
I have the following XML code that I'm trying to parse, but I'm sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.