I have the following example XML file containing text elements:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<Zoo>
<TotalAmountOfAnimals>18</TotalAmountOfAnimals>
<Animals><Animal xmlns="zoo">
<HEADER>
<Amount>7</Amount>
</HEADER>
</Animal>
</Animals>
</Zoo>
</Body>
</Envelope>
I now want to find a generic way to transform the text part of the XML into proper XML without losing the surrounding structure. My current approach is the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" media-type="text/xml"></xsl:output>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Animals">
<xsl:element name="Animals">
<xsl:value-of select="@*|node()" disable-output-escaping="yes"></xsl:value-of>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The outcome fits, but it's not generic as I have to give the specific name of the element (here: Animals) containing the text.
edit: The output for this case would then be:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<Zoo>
<TotalAmountOfAnimals>18</TotalAmountOfAnimals>
<Animals>
<Animal xmlns="zoo">
<HEADER>
<Amount>7</Amount>
</HEADER>
</Animal>
</Animals>
</Zoo>
</Body>
</Envelope>