How do I flatten text nodes and nested nodes?

2019-08-21 21:19发布

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)?

标签: xslt xslt-1.0
1条回答
对你真心纯属浪费
2楼-- · 2019-08-21 21:25

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>
查看更多
登录 后发表回答