Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code works the way it does because <code>xsl:value-of</code> retrieves the <a href="http://www.w3.org/TR/xpath#dt-string-value" rel="noreferrer">string-value</a> of the node set. </p> <p>To do what you want, I'm afraid that you'll have to code it explicitly:</p> <pre><code> &lt;xsl:template match="/"&gt; &lt;TestElement&gt; &lt;xsl:apply-templates mode="escape"/&gt; &lt;/TestElement&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*" mode="escape"&gt; &lt;!-- Begin opening tag --&gt; &lt;xsl:text&gt;&amp;lt;&lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;!-- Namespaces --&gt; &lt;xsl:for-each select="namespace::*"&gt; &lt;xsl:text&gt; xmlns&lt;/xsl:text&gt; &lt;xsl:if test="name() != ''"&gt; &lt;xsl:text&gt;:&lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;/xsl:if&gt; &lt;xsl:text&gt;='&lt;/xsl:text&gt; &lt;xsl:call-template name="escape-xml"&gt; &lt;xsl:with-param name="text" select="."/&gt; &lt;/xsl:call-template&gt; &lt;xsl:text&gt;'&lt;/xsl:text&gt; &lt;/xsl:for-each&gt; &lt;!-- Attributes --&gt; &lt;xsl:for-each select="@*"&gt; &lt;xsl:text&gt; &lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;xsl:text&gt;='&lt;/xsl:text&gt; &lt;xsl:call-template name="escape-xml"&gt; &lt;xsl:with-param name="text" select="."/&gt; &lt;/xsl:call-template&gt; &lt;xsl:text&gt;'&lt;/xsl:text&gt; &lt;/xsl:for-each&gt; &lt;!-- End opening tag --&gt; &lt;xsl:text&gt;&amp;gt;&lt;/xsl:text&gt; &lt;!-- Content (child elements, text nodes, and PIs) --&gt; &lt;xsl:apply-templates select="node()" mode="escape" /&gt; &lt;!-- Closing tag --&gt; &lt;xsl:text&gt;&amp;lt;/&lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;xsl:text&gt;&amp;gt;&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()" mode="escape"&gt; &lt;xsl:call-template name="escape-xml"&gt; &lt;xsl:with-param name="text" select="."/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:template&gt; &lt;xsl:template match="processing-instruction()" mode="escape"&gt; &lt;xsl:text&gt;&amp;lt;?&lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;xsl:text&gt; &lt;/xsl:text&gt; &lt;xsl:call-template name="escape-xml"&gt; &lt;xsl:with-param name="text" select="."/&gt; &lt;/xsl:call-template&gt; &lt;xsl:text&gt;?&amp;gt;&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;xsl:template name="escape-xml"&gt; &lt;xsl:param name="text"/&gt; &lt;xsl:if test="$text != ''"&gt; &lt;xsl:variable name="head" select="substring($text, 1, 1)"/&gt; &lt;xsl:variable name="tail" select="substring($text, 2)"/&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$head = '&amp;amp;'"&gt;&amp;amp;amp;&lt;/xsl:when&gt; &lt;xsl:when test="$head = '&amp;lt;'"&gt;&amp;amp;lt;&lt;/xsl:when&gt; &lt;xsl:when test="$head = '&amp;gt;'"&gt;&amp;amp;gt;&lt;/xsl:when&gt; &lt;xsl:when test="$head = '&amp;quot;'"&gt;&amp;amp;quot;&lt;/xsl:when&gt; &lt;xsl:when test="$head = &amp;quot;&amp;apos;&amp;quot;"&gt;&amp;amp;apos;&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;&lt;xsl:value-of select="$head"/&gt;&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;xsl:call-template name="escape-xml"&gt; &lt;xsl:with-param name="text" select="$tail"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; </code></pre> <p>Note that this solution ignores comment nodes, and inserts unneccessary namespace nodes (as <code>namespace::</code> 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). </p> <p>Also, this won't escape the <code>&lt;?xml ... ?&gt;</code> 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).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload