Xpath to get the whole xml tree structure

Is there a way using xpath to get the whole tree structure. For example, let's say it's xml

<a> <b> <c> data </c> </b> </a> 

I would like xpath to take all the contents of node, and the result should be

  <b> <c> data </c> </b> 

I have used VTD-XML and java so far to extract elements. This is the code I used.

  VTDGen vg = new VTDGen(); vg.setDoc(xmlString); vg.parse(true); VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath(xPath); String data = ap.evalXPathToString(); 
+4
source share
4 answers

Using

 /*/node() 

This selects all the child nodes of the top element of the XML document. The set of all these nodes has exactly the required subtree structure, because each selected element retains its entire subtree (of which this element is the top node).

XSLT Based Validation :

 <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:template match="/"> <xsl:copy-of select="/*/node()"/> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the provided XML document :

 <a> <b> <c> data </c> </b> </a> 

the XPath expression is evaluated and the selected nodes are displayed, which leads to the desired, correct result :

 <b> <c> data </c> </b> 
+3
source

Ideally, "// b" selects the nodes in the document from the current node that match the selection.

+2
source

Remember that XPath selects a node or set of nodes. Think of it as returning pointers to the original source tree. It does not return "the whole tree structure", it returns pointers to selected nodes in this structure. No matter what you choose, the entire structure is available for your program, moving from the selected nodes to others. This navigation can be done using additional XPath expressions or using a DOM-like navigation API or implicitly using operations such as serialization (usually when serializing a node) displays a full subtree based on that node).

+2
source

Use

 /descendant-or-self::node() 
0
source

Source: https://habr.com/ru/post/1390744/


All Articles