- 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 < and >.
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:
Example XSL:
what I want out at the end is this:
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.
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 < and >.
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="'<'"/>
<xsl:with-param name="with" select="concat('<', '')"/>
</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="'>'"/>
</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">
<literalExampleNode>
<literalExampleSubNode />
</literalExampleNode>
</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: