Parse HTML inside the CDATA text

2019-08-09 08:13发布

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>

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

But I am getting the output as

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

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

标签: xslt xslt-1.0
2条回答
在下西门庆
2楼-- · 2019-08-09 08:22

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>

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.

查看更多
甜甜的少女心
3楼-- · 2019-08-09 08:32

If your have to stay with XSLT 1.0 you have to to run two transformation passes.

  1. First one to copy your xml but remove the CDTA by generate the content with disable-output-escaping="yes" (See answer from @Martin Honnen)

  2. In second path you can access the html part.

But this may be only possible if the html part follow the roles for well formatted xml (xhtml). If not perhaps a input switch as in xsltproc may help to work with html e.g.:

 --html: the input document is(are) an HTML file(s)

See also: Convert an xml element whose content is inside CDATA

查看更多
登录 后发表回答