I'm looking for a generic algorithm which can flatten a XML file into a table, given multiple XPath expressions and all things i've tried failed due to the nature of available XPath engines implementations.
Given a XML:
<A Name="NameA">
<B Name="NameB1">
<C Name="NameC1"/>
<C Name="NameC2"/>
<C Name="NameC3"/>
</B>
<B Name="NameB2">
<C Name="NameC4"/>
<C Name="NameC5"/>
<C Name="NameC6"/>
</B>
and the following XPath expressions as input:
/A/@Name
/A/B/@Name
/A/B/C/@Name
The output should be a table in the following form:
NameA NameB1 NameC1
NameA NameB1 NameC2
NameA NameB1 NameC3
NameA NameB2 NameC4
NameA NameB2 NameC5
NameA NameB2 NameC6
I'm trying to get to this table with available Java XML packages such as javax.xml.xpath, jdom, etc.. to no avail.
It seems like the
XPath.evaluate("/A/B/C/@Name", doc, XPathConstants.NODESET);
code will return a "detached" Node which cannot be traversed.
I've tried many ways of recursion on XPath evaluated Nodes to no avail. Also thought of DFS traversal of the DOM tree, but again all XPath evaluators seem to return detached Nodes where node.getParent() will always return 'null'.
Any ideas for a "multi-XPath expression aware" algorithm which can keep track of nested XPath expressions?
I have a feeling this is possible easily with XSLT but my XSLT skills are pretty rusty...