How do I flatten text nodes and nested nodes?

余生长醉 提交于 2019-12-24 19:16:28

问题


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

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