How can I transform an XML document with XSLT such, that I read the input, apply a transformation (e.g. trim the leading and trailing whitespaces) to all elements in the document and return the XML document with its complete structure as it was?
(see also How can I trim space in XSLT without replacing repating whitespaces by single ones? for the trim-problem)
I started with the following code for copying all elements:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
which works fine. Now I wanted to apply the transformation by adding some lines:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()">
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="@* | node()" />
</xsl:call-template>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
But it seems that it is not allowed to add a “call-template”-Tag inside a “apply-templates”-Tag.
How can I copy the complete structure from the source-document into the target-document while applying the transformation to each element?
You can have separate templates for
text()and@*…Don’t forget to replace the template named “string-trim” with yours.