12

I just want to confirm whether we can insert html tags inside the xsl variable? example

<xsl:variable name="htmlContent">
<html>
<body>
hiiii
</body>
</html>
</xsl:variable>

if i use

<xsl:value-of select="$htmlContent"/>

I shoud get

<html>
<body>
hiiii
</body>
</html>

Is it possible? i have tried

<xsl:value-of disable-output-escaping="yes" select="$htmlContent"/>

Eventhough i am not getting the desired output

6
  • It should work. What output are you getting? Commented Nov 2, 2012 at 17:56
  • <htmlText>hii</htmlText> i have retrieved the value in the element called htmlText..I am getting like that Commented Nov 2, 2012 at 17:59
  • I think you aren't showing us everything. Where would <htmlText> come from? Commented Nov 2, 2012 at 18:04
  • <xsl:element name="htmlText"> <xsl:value-of disable-output-escaping="yes" select="$htmlContent"/> </xsl:element> I wrote like this and i shud get <htmlText><html> <body> hiiii </body> </html> </htmlText> Commented Nov 2, 2012 at 18:05
  • The code you are showing doesn't contain <xsl:element>. Please post a small but complete example that allows us to reproduce the problem. Commented Nov 2, 2012 at 18:07

1 Answer 1

19

Do not use value-of, which gets the text value of the selected node. Instead use copy-of, which copies the entire tree (nodes and all) into the output:

<xsl:copy-of select="$htmlContent"/>

Here is a full example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="htmlContent">
    <html><body>hiiii</body></html>
</xsl:variable>

<xsl:template match="/">
    <xsl:element name="htmlText">
        <xsl:copy-of select="$htmlContent"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

This will always produce the xml:

<htmlText>
    <html>
       <body>hiiii</body>
    </html>
</htmlText>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for quick response,I have used copy-of then it is elemenating html tags and printing the content..:(
You may also need to use exslt/msxsl node-set() to tell the parser that the variable is a result tree fragment.
can you please write sample code to add that node-set(), I am not that much expert in xslt :P
@nonnb, since he is not performing any node operations on the variable, he doesn't need a node-set and a result tree fragment is fine.
@SrivatsavaSesham, I have added a full, tested example. This should be working for you. (Make sure you are viewing the raw output of the transformation and not having it interpreted by a browser.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.