问题
I came across situation where XML tag has HTML code that needs to be parsed in XSLT. Here is the XML sample:
<note>
<text><p>This is a paragraph.</p><p>This is another paragraph.</p></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