如何展平文本节点和嵌套节点? [英] How do I flatten text nodes and nested nodes?

查看:35
本文介绍了如何展平文本节点和嵌套节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试展平元素的文本节点和嵌套的内联元素

I’m trying to flatten an element’s text nodes and nested inlined elements

<e>something <inline>rather</inline> else</e>

进入

<text>something </text>
<text-inline>rather</text-inline>
<text> else</text>

使用 e/text() 会返回两个文本节点,但我如何展平所有节点以便任意内联元素(甚至嵌套)?

Using e/text() would return both text nodes but how do I flatten all nodes in order for arbitrarily inlined elements (even nested)?

推荐答案

我不确定扁平化"是否是正确的术语.似乎您想要做的就是将一些文本节点更改为包含相同文本的元素.这可以通过匹配这些文本节点的模板来完成:

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>

演示:https://xsltfiddle.liberty-development.net/ncdD7n4

当然,如果您还想将 inline 重命名为 text-inline,您将需要另一个模板:

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>

这篇关于如何展平文本节点和嵌套节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆