Selection of plain text xml

advertisements

Given xml like this:

<container>
    <item>
        <xmlText>
            <someTag>
                <otherTag>
                    Text
                </otherTag>
            </someTag>
        </xmlText>
    </item>
<container>

I would like to select all text that is under item/xmlText. I would like to print all the content of this node with tags (someTag, otherTag).

I would prefer to handle with this with XPath, but this is part of Java program, so if there is such mechanism I could take it as well.


Use XSLT for this:

<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="/container/item/xmlText/node()"/>
 </xsl:template>
</xsl:stylesheet>

When this is applied on the provided XML document (corrected to be well-formed !!!):

<container>
    <item>
        <xmlText>
            <someTag>
                <otherTag>
                 Text
                </otherTag>
            </someTag>
        </xmlText>
    </item>
</container>

the wanted, correct result is produced:

<someTag>
   <otherTag>
                 Text
                </otherTag>
</someTag>