XSL node as string

Joined
Feb 26, 2009
Messages
2
Reaction score
0
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:
Joined
Feb 26, 2009
Messages
2
Reaction score
0
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top