XSLT: How to parse HTML embedded in XML tags

自闭症网瘾萝莉.ら 提交于 2019-12-10 23:57:48

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/52535431/xslt-how-to-parse-html-embedded-in-xml-tags

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