How can I convert the following XML to an escaped text using XSLT?
Source:
Your code works the way it does because xsl:value-of
retrieves the string-value of the node set.
To do what you want, I'm afraid that you'll have to code it explicitly:
<xsl:template match="/">
<TestElement>
<xsl:apply-templates mode="escape"/>
</TestElement>
</xsl:template>
<xsl:template match="*" mode="escape">
<!-- Begin opening tag -->
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<!-- Namespaces -->
<xsl:for-each select="namespace::*">
<xsl:text> xmlns</xsl:text>
<xsl:if test="name() != ''">
<xsl:text>:</xsl:text>
<xsl:value-of select="name()"/>
</xsl:if>
<xsl:text>='</xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:for-each>
<!-- Attributes -->
<xsl:for-each select="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>='</xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:for-each>
<!-- End opening tag -->
<xsl:text>></xsl:text>
<!-- Content (child elements, text nodes, and PIs) -->
<xsl:apply-templates select="node()" mode="escape" />
<!-- Closing tag -->
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="text()" mode="escape">
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="processing-instruction()" mode="escape">
<xsl:text><?</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text> </xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>?></xsl:text>
</xsl:template>
<xsl:template name="escape-xml">
<xsl:param name="text"/>
<xsl:if test="$text != ''">
<xsl:variable name="head" select="substring($text, 1, 1)"/>
<xsl:variable name="tail" select="substring($text, 2)"/>
<xsl:choose>
<xsl:when test="$head = '&'">&amp;</xsl:when>
<xsl:when test="$head = '<'">&lt;</xsl:when>
<xsl:when test="$head = '>'">&gt;</xsl:when>
<xsl:when test="$head = '"'">&quot;</xsl:when>
<xsl:when test="$head = "'"">&apos;</xsl:when>
<xsl:otherwise><xsl:value-of select="$head"/></xsl:otherwise>
</xsl:choose>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Note that this solution ignores comment nodes, and inserts unneccessary namespace nodes (as namespace::
axis will include all nodes inherited from parent). Regarding namespaces, however, the resulting quoted XML will be semantically equivalent to the example that you provided in your reply (since those repeated redeclarations don't really change anything).
Also, this won't escape the <?xml ... ?>
declaration, simply because it is not present in XPath 1.0 data model (it's not a processing instruction). If you actually need it in the output, you'll have to insert it manually (and make sure that encoding it specifies is consistent with serialization encoding of your XSLT processor).
I attempted to implement the answer provided by Pavel Minaev and want to point out that this is very dangerous for large strings as each character in the input string is recursed over individually, causing the recursion depth to quickly run out. I attempted to run it over a few lines of text and it caused a stack overflow (lol).
Instead, I use a template that does not need to examine each individual char, rather it will out put the text until it finds a string that needs to be replaced. This can then be used to escape characters:
<xsl:template name="Search-And-Replace">
<xsl:param name="Input-String"/>
<xsl:param name="Search-String"/>
<xsl:param name="Replace-String"/>
<xsl:choose>
<xsl:when test="$Search-String and contains($Input-String, $Search-String)">
<xsl:value-of select="substring-before($Input-String, $Search-String)"/>
<xsl:value-of select="$Replace-String"/>
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="substring-after($Input-String, $Search-String)"/>
<xsl:with-param name="Search-String" select="$Search-String"/>
<xsl:with-param name="Replace-String" select="$Replace-String"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Input-String"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Then its just a matter of calling that template for the char that you want to escape..
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="Hi I am a string & I am awesome"/>
<xsl:with-param name="Search-String" select="'&'"/>
<xsl:with-param name="Replace-String" select="'&amp;'"/>
</xsl:call-template>
In order to escape multiple characters in the one string, I used a wrapper template that uses variables...
<xsl:template name="EscapeText">
<xsl:param name="text" />
<xsl:variable name="a">
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="$text"/>
<xsl:with-param name="Search-String" select="'&'"/>
<xsl:with-param name="Replace-String" select="'&amp;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="b">
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="$a"/>
<xsl:with-param name="Search-String" select="'"'"/>
<xsl:with-param name="Replace-String" select="'&quot;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="c">
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="$b"/>
<xsl:with-param name="Search-String">'</xsl:with-param>
<xsl:with-param name="Replace-String" select="'&apos;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="d">
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="$c"/>
<xsl:with-param name="Search-String" select="'>'"/>
<xsl:with-param name="Replace-String" select="'&gt;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="e">
<xsl:call-template name="Search-And-Replace">
<xsl:with-param name="Input-String" select="$d"/>
<xsl:with-param name="Search-String" select="'<'"/>
<xsl:with-param name="Replace-String" select="'&lt;'"/>
</xsl:call-template>
</xsl:variable>
<!--this is the final output-->
<xsl:value-of select="$e"/>
</xsl:template>
This proved to be much safer for large strings as it no longer has to recurse for each individual character in the input string.
You can prevent the extra namespace nodes by adding a test in the namespace output:
<xsl:variable name="curnode" select="."/>
<xsl:for-each select="namespace::*">
<xsl:variable name="nsuri" select="."/>
<xsl:if test="$curnode/descendant-or-self::*[namespace-uri()=$nsuri]">
...
Why can't you just run
<xsl:template match="/">
<TestElement>
<xsl:copy-of select="." />
</TestElement>
</xsl:template>
Anyone who is concerned about licensing ambiguity when reusing code snippets from stack overflow may be interested in the following 3-clause BSD-licensed code, which appears to do what is requested by the original poster:
http://lenzconsulting.com/xml-to-string/
If you have access to it, I would recommend the Saxon extention serialize. It does exactly what you want it to do. If you don't want to do that, you'd have to manually insert the entity references as you build the document. It'd be brittle, but it would work for most documents:
<xsl:template match="/">
<TestElement>
<xsl:apply-templates/>
</TestElement>
</xsl:template>
<xsl:template match="*">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="node()"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
Most notably, this will probably break if your attributes have the double-quote character. It's really better to use saxon, or to use a user-written extention that uses a proper serializer if you can't.