XSLT: How to parse HTML embedded in XML tags

2019-07-15 06:20发布

I came across situation where XML tag has HTML code that needs to be parsed in XSLT. Here is the XML sample:

<note>
    <text>&lt;p&gt;This is a paragraph.&lt;/p&gt;&lt;p&gt;This is another paragraph.&lt;/p&gt;</text>
</note>

I want the embedded paragraph elements to be stored in different variables.

This is a paragraph. should be stored in one variable and This is another paragraph. should be stored in another variable.

Can you please help?

标签: xslt xslt-3.0
1条回答
forever°为你锁心
2楼-- · 2019-07-15 07:01

Parsing XML documents with parse-xml https://www.w3.org/TR/xpath-functions/#func-parse-xml or XML fragments with parse-xml-fragment https://www.w3.org/TR/xpath-functions/#func-parse-xml-fragment is supported in XSLT 3.0, in earlier versions you would have to rely on processor specific extension provided or implementable.

Your escaped code looks like an XHTML fragment so it should be parseable with parse-xml-fragment as in https://xsltfiddle.liberty-development.net/bFDb2CQ which does

<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:template match="text">
      <div>
          <xsl:variable name="contents" select="parse-xml-fragment(.)"/>
          <xsl:variable name="p1" select="$contents/p[1]"/>
          <xsl:variable name="p2" select="$contents/p[2]"/>
          <xsl:sequence select="$p1, $p2"/>
      </div>
  </xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答