Parse HTML inside the CDATA text

此生再无相见时 提交于 2019-12-11 13:43:07

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/16086755/parse-html-inside-the-cdata-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!