解析 CDATA 文本中的 HTML [英] Parse HTML inside the CDATA text

查看:31
本文介绍了解析 CDATA 文本中的 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CDATA 里面的数据要解析为 Html.

The data inside CDATA to be parsed as Html.

<?xml version="1.0" encoding="utf-8" ?>
<test>
  <test1>
    <![CDATA[ &lt;B&gt; Test Data1 &lt;/B&gt; ]]>
  </test1>

  <test2>
    <![CDATA[ &lt;B&gt; Test Data2 &lt;/B&gt; ]]>
  </test2>

  <test3>
    <![CDATA[ &lt;B&gt; Test Data3 &lt;/B&gt; ]]>
  </test3>
 </test>

从上面的输入 xml 中,我需要将输出解析为 html.

From the Above input xml I need the output to be parsed as html.

但我得到的输出为

<B>Test Data1</B>
<B>Test Data2</B>
<B>Test Data3</B>

但实际输出我需要用粗体显示文本.

But the actual output I need the text to be in bold.

**Test Data1
Test Data2
Test Data3**

输入来自外部系统.我们无法更改 CDATA 中的文本

The input is coming from external system.We could not change the text inside CDATA

推荐答案

解析为 HTML 只能使用扩展函数(或使用 XSLT 2.0 和用 XSLT 2.0 编写的 HTML 解析器),但如果您想创建 HTML 输出和想要将 testX 元素的内容输出为 HTML 那么你可以使用 eg

Parsing as HTML is only possible with an extension function (or with XSLT 2.0 and an HTML parser written in XSLT 2.0) but if you want to create HTML output and want to output the contents of the testX elements as HTML then you can do that with e.g.

<xsl:template match="test/*[starts-with(local-name(), 'test')]">
  <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>

但是请注意,disable-output-escaping 是一个可选的序列化特性,并非在所有用例中的所有 XSLT 处理器都支持.例如,Mozilla 浏览器中的客户端 XSLT 不受支持.

Note however that disable-output-escaping is an optional serialization feature not supported by all XSLT processors in all use cases. For instance with client-side XSLT in Mozilla browsers it is not supported.

这篇关于解析 CDATA 文本中的 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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