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

mon*_*nny 2 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>
Run Code Online (Sandbox Code Playgroud)

如何提取"内部"HTML文档; 将其渲染为CDATA并包含在我的输出文档中?

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

我试过这个:

<xsl:template match="document">
<xsl:value-of select="*"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

但这只会呈现文本节点.

我试过这个:

<xsl:template match="document">
<![CDATA[
<xsl:value-of select="*"/>
]]>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

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

&lt;xsl:value-of select="*"/&gt;
Run Code Online (Sandbox Code Playgroud)

我试过这个:

<xsl:output method="xml" indent="yes" cdata-section-elements="document"/>
[...]
<xsl:template match="document">
<document>
<xsl:value-of select="*"/>
</document>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这确实插入了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>
Run Code Online (Sandbox Code Playgroud)

C. *_*een 11

你需要在这里清理两个混乱.

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

其次,cdata-section-elements属性on 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>
Run Code Online (Sandbox Code Playgroud)

但更快的方式就是下面的解决方案(娇气的读者,现在停止阅读),我的朋友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> 
Run Code Online (Sandbox Code Playgroud)