Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2005, 10:12 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to literal HTML Issue

On another post, I received a great response from Michael Kay to my question concerning displaying XML data with HTML tags:

Quote:
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.[list], <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
 
Old October 4th, 2005, 01:58 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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.
Code:
<?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
 
Old October 4th, 2005, 08:24 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old October 4th, 2005, 08:35 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML tags issue.. atulshin XSLT 2 October 21st, 2008 03:57 AM
XML Transformation - General HTML Issue tclotworthy XSLT 2 February 27th, 2007 03:42 PM
Compiled HTML Issue pvasudevan HTML Code Clinic 9 August 16th, 2006 07:29 AM
Issue sending HTML file as attachment in EMail kdhawan_forum VB Components 0 April 8th, 2005 02:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.