要消除内部 para 元素,但在逐项列表中的后续 para 元素之间放置一个 br 元素,给定的 xml 如下所示:
<listitem>
<para>The application of power invokes the POR state machine(PORSM). During
this time, the following registers are initialized:</para>
.
.
.
<para>In addition, the JTAG interface is disabled and the SSBD is
open.</para>
</listitem>
我写了一个如下的模板:
<xsl:template match="listitem/para">
<xsl:value-of select="."/>
<xsl:if test="count(./following-sibling::para) > 0">
<xsl:element name="br"/>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
我得到了 br,但是初始的 para 元素被重复了:
The application of power invokes the POR state machine
(PORSM). During this time, the following registers are
initialized:
<br/>The application of power invokes the POR state machine
(PORSM). During this time, the following registers are
initialized:
我知道这是一个新手问题,但是有人可以解释为什么我会重复当前的文本节点吗?
谢谢
拉斯
最佳答案
第一个副本来自
<xsl:value-of select="."/>
第二个来自
<xsl:apply-templates/>
它将模板应用于 para
元素的所有子节点。子节点包括文本节点和元素节点,如果您没有指定显式模板来匹配 text()
,则文本节点的默认模板会将文本输出到结果树。
关于xml - 为什么上下文节点获得两次输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844596/