将“嵌入式” XML文档转换为XSLT(1.0)中的CDATA输出 [英] Convert 'embedded' XML doc into CDATA output in XSLT (1.0)

查看:87
本文介绍了将“嵌入式” XML文档转换为XSLT(1.0)中的CDATA输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的输入XML文档:

Given an input XML document like this:

<?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并包含在我的输出文档中?

How I can extract that 'inner' HTML document; render it as CDATA and include in my output document ?

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

So the output document will be an HTML document; which contains a text-box showing the elements as text (so it will be displaying the 'source-view' of the inner document).

我已经尝试过:

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

但这只会渲染文本节点。

But this only renders the Text Nodes.

我已经尝试过:

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

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

But this escapes the actual XSLT and I get:

&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部分,但输出仍然只包含文本(剥离的元素):

This does insert a CDATA section, but the output still contains just text (stripped elements):

<?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>


推荐答案

您需要在此处清除两个混淆。

There are two confusions you need to clear up here.

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

First, you probably want xsl:copy-of rather than xsl:value-of. The latter returns the string value of an element, the former returns a copy of the element.

第二, xsl:output cdata-section-elements 属性c $ c>影响文本节点的序列化,但不影响元素和属性的序列化。一种获得所需内容的方法是按照以下步骤(未经测试)自己对HTML进行序列化:

Second, the cdata-section-elements attribute on xsl:output affects the serialization of text nodes, but not of elements and attributes. One way to get what you want would be to serialize the HTML yourself, along the lines of the following (not tested):

<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)向我展示从 xsl:output 删除 cdata-section-elements 属性,然后将模板替换为文档元素,其中:

But the quicker way would be something like the following solution (squeamish readers, stop reading now), pointed out to me by my friend Tommie Usdin. Drop the cdata-section-elements attribute from xsl:output and replace your template for the document element with:

<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> 

这篇关于将“嵌入式” XML文档转换为XSLT(1.0)中的CDATA输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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