|
Subject:
|
XML to literal HTML Issue
|
|
Posted By:
|
kwilliams
|
Post Date:
|
10/3/2005 10:12:55 AM
|
On another post, I received a great response from Michael Kay to my question concerning displaying XML data with HTML tags:
quote: What methods exist with 1.0 that display tags within an XML tag as literal HTML? Example: <root> <element> <child> <h1>TEST</h1> <p>This is a test</p> <p>This is only a test</p> </child> </element> </root>
transforms to this using an XSLT stylesheet:
<html> <body> <h1>TEST</h1> <p>This is a test</p> <p>This is only a test</p> </body> </html>
Concerning your question:
<xsl:template match="child"> <xsl:copy-of select="*"/> </xsl:template>
Michael Kay
This method has worked great except for one little thing. The <p> tags don't display as paragraphs, but all of the other HTML tags work fine (i.e. <ul>, <li>, <br />, etc.). I've tried two different methods, but neither of them work. I'm adding examples of my XML doc, XSLT stylesheet, and HTML output at the bottom of this post. If anyone can see what I'm doing wrong, and steer me in the right direction, it would be greatly appreciated. Thanks.
P.S. I've also tried changing the output from "xml" to "html", but that also had no effect on the <p>...</p> tags.
XML Doc: <root> <pageinfo> <description> <p>This is an example description.</p> <p>It contains no real data.</p> </description> </pageinfo> </root>
METHOD #1 XSLT Stylesheet: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="root/pageinfo"> <xsl:copy-of select="description" /> </xsl:for-each>
HTML Output: <html> <body> <description> <p>This is an example description.</p> <p>It contains no real data.</p> </description> </body> </html>
Page Display: This is an example description. It contains no real data.
METHOD #2 XSLT Stylesheet: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="root/pageinfo/description"> <xsl:copy-of select="p" /> </xsl:for-each>
HTML Output: <html> <body> <p>This is an example description.</p> <p>It contains no real data.</p> </body> </html>
Page Display: This is an example description. It contains no real data.
KWilliams
|
|
Reply By:
|
joefawcett
|
Reply Date:
|
10/4/2005 1:58:50 AM
|
Are you trying to server HTML or XHTML? If HTML then you need xsl:output method as HTML. As description is not a recognised HTML element then it and any children will just show plain text.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Simple Transform</title>
</head>
<body>
<xsl:apply-templates select="root/pageinfo/description/p" />
</body>
</html>
</xsl:template>
<xsl:template match="p">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
--
Joe
|
|
Reply By:
|
kwilliams
|
Reply Date:
|
10/4/2005 8:24:53 AM
|
I'm using it in a transformation on the server using the transformNode method. I've tried the option of changing it to : <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="root/pageinfo/description"> <xsl:copy-of select="p" /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
So I also tried your suggested code, but it had no affect. It still all showed up in one paragraph. If I enter <br /> tags instead of <p> tags, the paragraphs break properly. And if I use any other HTML element, they work fine. It's only the <p> tags that don't display correctly. When I view the source code of the page, they are imbedded properly, but it's still not being displayed properly. Any other suggestions would be great. Thanks.
KWilliams
|
|
Reply By:
|
joefawcett
|
Reply Date:
|
10/4/2005 8:35:49 AM
|
Again you seem to be specifying XML rather than HTML.
Are you saying that if you view source it shows the correct HTML? If so then as I said you must specify HTML in your xsl:output method.
--
Joe
|