XSL node as string

Discussion in 'XML' started by ben_leedham, Feb 26, 2009.

  1. ben_leedham

    ben_leedham

    Joined:
    Feb 26, 2009
    Messages:
    2
    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: Feb 26, 2009
    ben_leedham, Feb 26, 2009
    #1
    1. Advertising

  2. ben_leedham

    ben_leedham

    Joined:
    Feb 26, 2009
    Messages:
    2
    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.
     
    ben_leedham, Mar 2, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. viveknatani@gmail.com
    Replies:
    0
    Views:
    1,568
    viveknatani@gmail.com
    Feb 13, 2006
  2. CB
    Replies:
    1
    Views:
    596
    Johannes Koch
    Oct 2, 2003
  3. schaf@2wire.ch
    Replies:
    1
    Views:
    3,637
    A. Bolmarcich
    May 27, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,459
    Dimitre Novatchev
    Aug 24, 2006
  5. jiing
    Replies:
    0
    Views:
    2,358
    jiing
    Apr 27, 2007
Loading...

Share This Page