Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL node as string

Reply
Thread Tools

XSL node as string

 
 
ben_leedham ben_leedham is offline
Junior Member
Join Date: Feb 2009
Posts: 2
 
      02-26-2009
Hi,

I have some XML I would like to transform to html, I have got the transformation working ok but I want to use some of the XML as a literal example of XML within the html page.

I can get the literal nodes I want as an example using the xsl:copy-of tag, but obviously this doesn't display properly in HTML.

I know I can display the examples as long as I can replace the < and > characters with &lt; and &gt;.

I have found several examples of replace functions on the internet that replace characters within nodes, but when I have tried to use them to replace the angle brackets I have had no success.

Here is what I have been trying:

Example XML:
Code:
<ops>
    <op>
        <examples>
            <example>
                <literalExampleNode>
                    <literalExampleSubNode />
                </literalExampleNode>
            </example>
        </examples>
    </op>
</ops>
Example XSL:
Code:
<xsl:template name="content">
    <div id="content">
        <xsl:for-each select="/ops/op">
            <div id="op">
                <xsl:for-each select="examples/example">
                    <div id="example">
                        <!-- get nodes to filter -->
                        <xsl:variable name="unchanged">
                            <xsl:copy-of select="*" />
                        </xsl:variable>
                        <!-- filter left angle bracket into variable -->
                        <xsl:variable name="leftBracketReplaced">
                            <xsl:call-template name="replace-string">
                                <xsl:with-param name="text" select="$unchanged"/>
                                <xsl:with-param name="replace" select="'&lt;'"/>
                                <xsl:with-param name="with" select="concat('&lt;', '')"/>
                            </xsl:call-template>
                        </xsl:variable>
                        <!-- filter right angle bracket into variable -->
                        <xsl:variable name="rightBracketReplaced">
                            <xsl:call-template name="replace-string">
                                <xsl:with-param name="text" select="$leftBracketReplaced"/>
                                <xsl:with-param name="replace" select="'>'"/>
                                <xsl:with-param name="with" select="'&gt;'"/>
                            </xsl:call-template>
                        </xsl:variable>
                        <!-- put out filtered variable -->
                        <xsl:value-of select="$rightBracketReplaced" />
                    </div>
                </xsl:for-each>
            </div>
        </xsl:for-each>
    </div>
</xsl:template>

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
        <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$with"/>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="with" select="$with"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
what I want out at the end is this:
Code:
<div id="content">
    <div id="op">
        <div id="example">
            &lt;literalExampleNode&gt;
                &lt;literalExampleSubNode /&gt;
            &lt;/literalExampleNode&gt;
        </div>
    </div>
</div>
I know it looks ugly but it displayes properly in HTML.

I am not getting that though, I am getting my html structure but with a load of question marks where I want my example to be.

Any thoughts would be greatly appreciated.
 

Last edited by ben_leedham; 02-26-2009 at 02:34 PM..
Reply With Quote
 
 
 
 
ben_leedham ben_leedham is offline
Junior Member
Join Date: Feb 2009
Posts: 2
 
      03-02-2009
From what I have found out it isn't possible to do what I wanted to do, that is, take a set of node and transform them as if they were text.

What you can do however is surround the nodes you want to use as an example with a <![CDATA[ declaration and as long as you output is html and you put out the CDATA section in a pre tag it is all done for you.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find node in TreeView by using string(the same as Node.Text) ? jiing ASP .Net 0 04-27-2007 02:34 AM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
XSL HOWDOI use attribute of current node to select a different node? CB XML 1 10-02-2003 02:37 PM



Advertisments