首页 > 解决方案 > 使用 XSL 文件解析 CDATA 块内的 HTML

问题描述

我想解析 CDATA 块内的 HTML 以获取<p>标签,并将每个标签输出到单独的表行中。但是我不太明白,想知道是否有人可以帮助我?

我一直在尝试解析 HTML,但无法弄清楚如何解析它而不是简单地将其视为字符数据。我很确定我不能用 XSL 1.0 做到这一点,如果需要我可以使用 2.0。

XML

<XML_FILE>
  <NOTE>
    <TEXT TITLE="TEST">
      <![CDATA[<p>first p tag and <strong>bold</strong></p><p>second p tag and  <u>underline</u></p>]]>
    </TEXT>
  </NOTE>
</XML_FILE>

XSL

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="NOTE">
    <div class="tableWrapper">
      <table class="body">
        <xsl:apply-templates select="TEXT"/>
      </table>
    </div>
  </xsl:template>

  <xsl:template match="TEXT">
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:template>

</xsl:stylesheet>

输出

<div class="tableWrapper">
   <table class="body"><p>first p tag and <strong>bold</strong></p><p>second p tag and <u>underline</u></p></table>
</div>

期望的输出

<div class="tableWrapper">
   <table class="body">
      <tr><td><p>first p tag and <strong>bold</strong></p></td></tr>
      <tr><td><p>second p tag and <u>underline</u></p></td></tr>
   </table>
</div>

提供所需输出的最终样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="html" indent="yes" html-version="5"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="XML_FILE">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="NOTE">
    <div class="tableWrapper">
      <table class="body">
        <xsl:apply-templates select="parse-xml-fragment(TEXT)/node()"/>
      </table>
    </div>
  </xsl:template>

  <xsl:template match="p">
      <tr>
          <td>
              <xsl:next-match/>
          </td>
      </tr>
  </xsl:template>

</xsl:stylesheet>

标签: htmlxmlxslt

解决方案


XSLT 3.0 有一个函数parse-xml-fragment()可以解决这个问题。

在早期的 XSLT 版本中没有任何等价物,尽管您可能会找到可以帮助您的供应商扩展。大多数处理器允许您编写自己的外部函数,您可以从 XSLT 代码调用这些函数,并且您可以编写这样的函数,将 CDATA 内容传递给外部 XML 解析器以转换为树结构。


推荐阅读