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 256423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:00:46+00:00 2026-05-11T22:00:46+00:00

Ive got a large XML set, which I would like to run some xpath

  • 0

Ive got a large XML set, which I would like to run some xpath on to make into a much smaller sub-set.
Basically, I have this type of layout:

<root>
  <item>
    <collection1></collection1>
    <collection2></collection2>
    <collection3></collection3>
    ...
    <collection55></collection55>
    <name>item name</name>
    <timestamp>47398743598</timestamp>
    <another1></another1>
    <another2></another2>
    ...
  </item>
  <item>
   ...
  </item>
</root>

In other words, heaps of item nodes, and lots of other junk nodes that I dont care about.

I would like to run some xpath, to get that down to:

<root>
  <item>
    <name>item name</name>
    <timestamp>47398743598</timestamp>
  </item>
  <item>
   ...
  </item>
</root>

I have currently this type of thing:

//item/name

which only gets the name nodes,

so then Ive been trying this type of thing:

//item/name/parent::item

which gets the name nodes, and its parent (which is the item node) but also all of the sibling nodes of the name node, which is what Im trying to avoid!

Any help would be greatly appreciated

Cheers,
Mark

  • 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-11T22:00:46+00:00Added an answer on May 11, 2026 at 10:00 pm

    First off: You can’t use XPath to get an XML document “down to something”. You can use it to select nodes, that’s all. If you want to change the XML document, use XSLT.

    This expression:

    //item/name/parent::item
    

    does not select “the name nodes, and its parent”, it selects the parent nodes of <name> nodes, and nothing else.

    Strictly speaking, it selects all <item> nodes that happen to be parent of a <name> node that is itself child of an <item> node. Which is equivalent to using just "//item", when you think about it.

    There is no way to select a structure of nodes. You can only select a list of nodes – a node set. You could then traverse those nodes and find out about their position in the document, but the node set itself is flat.

    I think you need to explain more closely what you are trying to do. I could write an XSL transformation that does what you seem to intend, but unless I am sure what you intend… 😉

    EDIT:

    Here is one minimalistic XSLT 1.0 approach that would do it.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="root | item | name | timestamp">
        <xsl:copy>
          <xsl:apply-templates select="*" />
          <xsl:if test="count(*) = 0">
            <xsl:value-of select="text()" />
          </xsl:if>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="* | text()" />
    
    </xsl:stylesheet>
    

    Output for your sample (indentation mine):

    <root>e
      <item>
        <name>item name</name>
        <timestamp>47398743598</timestamp>
      </item>
      <item>
       ...
      </item>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 187k
  • Answers 187k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT l.column_name, l.position FROM all_constraints n JOIN all_cons_columns l ON… May 12, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer There are always side effects :-) In this case it… May 12, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Not as far as I know. Doesn't look like there's… May 12, 2026 at 5:25 pm

Related Questions

I've got some very large XML files which I read using a System.Xml.Serialization.XmlSerializer .
I've got a very large xml data set that is structured like the following:
I've got a large xml document in a string. What's the best way to
I've got a hibernate-based application which uses DBUnit for unit testing. We have an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.