p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Retrieve the escaped node content (http://p2p.wrox.com/showthread.php?t=33394)

carlos.bravo September 14th, 2005 11:16 AM

Retrieve the escaped node content
 
Hello all:

I want to retrieve the value of a node that has children, and escape it (transform the "<" to "&lt;", and ">" to "&gt;"), something like:

XML:

<root>
  <node>
     <nodechild>A</nodechild>
     <nodechild>B</nodechild>
     <nodechild>C</nodechild>
  </node>
</root>


Desired output:

&lt;root&gt;
  &lt;node&gt;
     &lt;nodechild&gt;A&lt;/nodechild&gt;
     &lt;nodechild&gt;B&lt;/nodechild&gt;
     &lt;nodechild&gt;C&lt;/nodechild&gt;
  &lt;/node&gt;
&lt;/root&gt;


I've tried to retrieve directly the root value with:


<xsl:value-of select="." />


... but the output doesn't contain the "<,>" characters, only blank spaces.

...any ideas?

Thanks in advance ;)

Carlos.


mhkay September 14th, 2005 01:27 PM

Actually, the value of the node does not have any angle brackets in it, so there are none to escape. You need to serialize the node, and then escape its serialization. With a saxon extension function you can do


<xsl:output method="xml"/>

<xsl:value-of select="saxon:serialize($node)"/>

I can't immediately think of any other solution, at least not within XSLT. It's more common for people to want to remove one level of escaping than to add one: which can be achieved using disable-output-escaping.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

mhkay September 14th, 2005 01:29 PM

Of course the other way you can do this is to serialize "by hand", something like

<xsl:template match="*">
&lt;<xsl:value-of select="name()"/>&gt;
<xsl:apply-templates/>
&lt;/<xsl:value-of select="name()"/>&gt;
</xsl:template>

Not too difficult if there are no attributes and namespaces around.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

carlos.bravo September 15th, 2005 01:45 AM

Thanks Michael:

I already knew the second solution, but in my case, the attributes are neccessary :-S. I'll try with the node serialization.


rushman September 16th, 2005 03:23 PM

Hi!

Following Mr. Kay's "hand serialization", here's a version that include attributes.

I leave it to you to indent it if you want.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:text>&amp;lt;</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:apply-templates select="@*"/>
        <xsl:text>&amp;gt;</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>&amp;lt;/</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>&amp;gt;</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:stylesheet>

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.


All times are GMT -4. The time now is 11:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.