通过XSLT将PHP,XML转换为HTML,使CDATA内的HTML编码 [英] PHP, XML to HTML through XSLT makes HTML inside CDATA encoded

查看:82
本文介绍了通过XSLT将PHP,XML转换为HTML,使CDATA内的HTML编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要这个问题的答案。我正在使用XML创建页面,然后使用XSLT将其生成到Web页面的项目。这是一个代码示例:

I really need an answer to this question. I am working on a project which uses XML to make pages, then XSLT to produce it to a web page. Here is a code sample:

 public function transform ($xml) {
  $proc = new XSLTProcessor;
  $proc->importStyleSheet ($this->xsl);
  $output = $proc->transformToXML ($xml);
  return $output;
 }

$ xml包含XML格式的网页,例如:

the $xml contains the web page in XML format, for example:

<?xml version="1.0" encoding="utf-8"?>
<page>
 <meta>
  <language>en</language>
  <title>Main page</title>

 <content>
<![CDATA[
  lot's of content here along with <p>html</p>.
]]>
 </content>

</page>

因此,网页采用XML格式并传递给了transform函数,该函数将XSL文件加载到将XML转换为HTML网页。但是,上述脚本将无法正确加载内容标签,因为XSLT由于某种原因无法理解CDATA,并将HTML编码为实体。

So, the web page is in XML and passed to the transform function, which loads an XSL file to transform the XML to a HTML web page. However, the above scripts will fail to load the content tag properly, because XSLT for some reason doesn't understand CDATA, and encodes the HTML to entities.

因此,问题是,如何使用XSLT将HTML输出为HTML而不将其编码为实体?

So, the question is, how can I output, using XSLT, the HTML as HTML without getting it encoded into entities?

推荐答案

我是确保您的XSLT中没有这样的内容:

I'm sure you don't have something like this in your XSLT:

<xsl:template match="content">
  <xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>

XSLT可以很好地理解 CDATA。更确切地说,它根本不涉及CDATA,这是底层XML DOM解析器的任务,该语法从中获取文本值。

XSLT "understands" CDATA perfectly well. More exactly - it not concerned with CDATA at all, this is the task of the underlying XML DOM parser which makes a text value out of it.

从XSLT的角度来看,无法知道字符串是否是

From an XSLT point of view, there is no way of knowing whether the string

"<div>bla &amp; bla</div>"

出现

<xml>&lt;div&gt;bla &amp;amp; bla&lt;/div&gt;</div>

<xml><![CDATA[<div>bla &amp; bla</div>]]></div>

CDATA只是序列化的便利。生成的信息集/ DOM 相同。并且,除非您禁用输出转义,否则XSLT会根据上述字符串正确生成以下值:

CDATA is merely a serialization convenience. The resulting info set/DOM is the same. And unless you disable output escaping, XSLT correctly produces the following value from the above string:

&lt;div&gt;bla &amp;amp; bla&lt;/div&gt;

这是您在呈现的页面上看到HTML代码的原因。

Which is the reason for the fact that you see HTML code on the rendered page.

这篇关于通过XSLT将PHP,XML转换为HTML,使CDATA内的HTML编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆