[thelist] [XSLT/PHP] problems outputting HTML embedded within an XML doc

Aredridel aredridel at nbtsc.org
Mon Jul 14 19:10:14 CDT 2003


On Mon, 2003-07-14 at 12:20, Jacques Capesius wrote:
> Hi folks,
> 
> I'm trying to embed HTML tags within an XML document, and then transform the
> document to html using XSL. The problem I'm having is that during the
> transformation, the html tags in the XML document are getting stripped out,
> and I don't want this to happen.
> 
> For example,
> 
> the following XML document....
> 
> ---------------------------------------------
> <?xml version='1.0'?>
> <page>
>   <content>
>   <p>1st paragraph</p>
>   <p>
>     2nd paragraph
>     <ul>
>           <li>1st item</li>
>      <li>2nd item</li>
>         </ul>
>   </p>
>   </content>
> </page>
> 
> ---------------------------------------------
> ...when transformed by the following XSL template...
> 
> <?xml version='1.0'?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="html" indent="no" encoding="ISO-8859-1" />
> 
>   <xsl:template match="page">
>     <xsl:value-of select="content" />
>   </xsl:template>
> 
> </xsl:stylesheet>


You need some different rules.  "value-of" returns stripped text, not
tags.

Try this:

<xsl:template match='page'>
  <xsl:apply-templates select='content/node()|content/@*' />
</xsl:template>

<xsl:template match='node()|@*'>
  <xsl:copy>
    <xsl:apply-templates select='node()|@*' />
  </xsl:copy>
</xsl:template>

That's a recursive template that overrides the "default" template (which
only copies node()s, not atrributes (@*), and a start-rule that will
apply that rule to the contents of any "page/content/"

One thing you might consider is putting the HTML tags in the proper
namespace -- xmlns:xhtml="http://www.w3.org/1999/xhtml", then writing
rules that match xhtml:node() instead of just any, and telling xsl to
output those tags without prefix on the output side.

> ---------------------------------------------
> ...the html output is this:
> 
> 
>   1st paragraph
>   
>     2nd paragraph
>     
>      1st item
>      2nd item
> 
> -----------------------------------------------
> ...what I want it to output is this:
> 
> <p>1st paragraph</p>
>   <p>
>     2nd paragraph
>     <ul>
>      <li>1st item</li>
>      <li>2nd item</li>
>     </ul>
>   </p>
> 
> is there anything I can do within XSL that will prevent the html tags from
> being stripped out?
> is there something I can do within PHP to accomplish same?
> to transform the doc, I'm using..
> 
> $xp = xslt_create();
> $result = @xslt_process($xp, $xml, $xsl);
> echo $result;
> xslt_free($xp);
> 
> TIA for any help you can give



More information about the thelist mailing list