i'm again. I've a new problem.
I like to strip/reduce an xml structure to only needed elements.
To explain the problem i built a simplyfied random structure.
<ROOT>
<DATA>
<ALLOC>
<TYPE>Test</TYPE>
<NAME>something text</NAME>
<VALUE>4711</VALUE>
</ALLOC>
<ALLOC>
<TYPE>Test</TYPE>
<NAME>something text</NAME>
<VALUE>4712</VALUE>
</ALLOC>
<ALLOC>
<TYPE>Test</TYPE>
<NAME>something text</NAME>
<VALUE>4713</VALUE>
</ALLOC>
</DATA>
<SOURCE>
<CONNECTION>
<TYPE>SQL</TYPE>
<VALUE>jdbc</VALUE>
<CSTRING>jdbc string</CSTRING>
</CONNECTION>
<CONNECTION>
<TYPE>CSV</TYPE>
<VALUE>CSV</VALUE>
<CSTRING></CSTRING>
</CONNECTION>
</SOURCE>
</ROOT>
Requiered Elements are e.g.:
/ROOT[1]/DATA[1]/ALLOC[2]/VALUE[1]
/ROOT[1]/SOURCE[1]/CONNECTION[1]/CSTRING[1]
The requiered Elements Statements comes from java with xmlassert.equal > xmldiff
Now i have to strip the xml structure, to requiered elements, but keeping the xml structure (xpath) of elements.
The desired output is:
<ROOT>
<DATA>
<ALLOC>
<VALUE>4712</VALUE>
</ALLOC>
</DATA>
<SOURCE>
<CONNECTION>
<CSTRING>jdbc string</CSTRING>
</CONNECTION>
</SOURCE>
</ROOT>
The real structure is huge (minimum 6x A4 Pages if you would print it), complex and has multilevels. The requested Elements are also dynamically.
I spent the last hours with reading threads in a lot of fourms, tries with a lot of amount of different xslt's and reading of more threads.
How can i do that?
Thank you so much in advance.
This is a short and simple XSLT 1.0 generic solution:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
For every element in the XML document, its XPath expression (in the style specified in the question) is produced. This element is:
Genericity of the solution:
The input XPath expressions can be passed as an
<xsl:param>on the invocation of the transformation, or can be in an XML file, whose URI is passed as parameter to the transformation.Note:
For a more involved and elegant way of producing an XPath expression for every type of node, see this answer.