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
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:
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.
Output for your sample (indentation mine):