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:
<
xmlns
:
='
'
='
'
>
</
>
<?
?>
&
<
>
"
'
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 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).