Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2005, 11:16 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old September 14th, 2005, 01:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old September 14th, 2005, 01:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old September 15th, 2005, 01:45 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael:

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

 
Old September 16th, 2005, 03:23 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve numbered node path from xml jeft0nes XSLT 6 July 20th, 2007 03:59 PM
How Retrieve data of a node or from an element chandu.mca007 XML 1 December 16th, 2006 03:11 PM
How to use XPath to retrieve a node tag rs:data? y10k XML 1 May 5th, 2006 01:11 PM
constructing node-set content to xsl:variable MrWay XSLT 3 February 28th, 2006 10:39 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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