在XSLT(1.0)中将“嵌入式”XML文档转换为CDATA输出

时间:2012-09-12 13:02:02

标签: html xml xslt cdata

给出一个像这样的输入XML文档:

<?xml version="1.0" encoding="utf-8"?>
<title> This contains an 'embedded' HTML document </title>
<document>
<html>
<head><title>HTML DOC</title></head>
<body>
Hello World
</body>
</html>
</document>
</root>

如何提取“内部”HTML文档;将其呈现为CDATA并包含在我的输出文档中?

因此输出文档将是一个HTML文档;其中包含一个文本框,将元素显示为文本(因此它将显示内部文档的“源视图”)。

我试过这个:

<xsl:template match="document">
<xsl:value-of select="*"/>
</xsl:template>

但这只会呈现文本节点。

我试过这个:

<xsl:template match="document">
<![CDATA[
<xsl:value-of select="*"/>
]]>
</xsl:template>

但这逃脱了实际的XSLT,我得到了:

&lt;xsl:value-of select="*"/&gt;

我试过这个:

<xsl:output method="xml" indent="yes" cdata-section-elements="document"/>
[...]
<xsl:template match="document">
<document>
<xsl:value-of select="*"/>
</document>
</xsl:template>

这会插入CDATA部分,但输出仍然只包含文本(剥离元素):

<?xml version="1.0" encoding="UTF-8"?>
<html>
   <head>
      <title>My doc</title>
   </head>
   <body>
      <h1>Title: This contains an 'embedded' HTML document </h1>
      <document><![CDATA[
                                                HTML DOC

                                                                Hello World

                                ]]></document>
   </body>
</html>

1 个答案:

答案 0 :(得分:11)

这里有两个需要清理的混淆。

首先,您可能需要xsl:copy-of而不是xsl:value-of。后者返回元素的字符串值,前者返回元素的副本。

其次,cdata-section-elements上的xsl:output属性会影响文本节点的序列化,但不影响元素和属性的序列化。获得所需内容的一种方法是自行序列化HTML,按照以下方式(未经测试):

<xsl:template match="document/descendant::*">
  <xsl:value-of select="concat('&lt;', name())"/>
  <!--* attributes are left as an exercise for the reader ... *-->
  <xsl:text>&gt;</xsl:text>
  <xsl:apply-templates/>
  <xsl:value-of select="concat('&lt;/', name(), '>')"/>
</xsl:template>

但更快捷的方式就是以下解决方案(娇气的读者,现在停止阅读),我的朋友Tommie Usdin向我指出。从cdata-section-elements中删除xsl:output属性,并将document元素的模板替换为:

<xsl:template match="document">
  <document>
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
    <xsl:copy-of select="./html"/>
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
  </document>
</xsl:template>