问题
I’m trying to flatten an element’s text nodes and nested inlined elements
<e>something <inline>rather</inline> else</e>
into
<text>something </text>
<text-inline>rather</text-inline>
<text> else</text>
Using e/text()
would return both text nodes but how do I flatten all nodes in order for arbitrarily inlined elements (even nested)?
回答1:
I am not sure "flatten" is the right term for this. It seems all you want to do is change some text nodes into elements containing the same text. This can be done by a template matching these text nodes:
<xsl:template match="e/text()">
<text>
<xsl:copy/>
</text>
</xsl:template>
Demo: https://xsltfiddle.liberty-development.net/ncdD7n4
Of course, if you also want to rename inline
to text-inline
, you will need another template for that:
<xsl:template match="inline">
<text-inline>
<xsl:apply-templates />
</text-inline>
</xsl:template>
来源:https://stackoverflow.com/questions/56724633/how-do-i-flatten-text-nodes-and-nested-nodes