【发布时间】:2017-10-13 09:17:43
【问题描述】:
在我的 xsl 中,已经为 para、graphic 等元素定义了模板。示例如下:
<xsl:template match="para">
<fo:block>
<xsl:apply-templates />
</fo:block>
</xsl:template>
但是我想在最内层添加一个额外的节点以防特定属性值。例如,如果元素的属性值为 changeStatus = new/changed,我需要在其他节点内添加 'fo:change-bar-begin' 元素。示例 xml:
<para changeStatus="new">
This is a paragraph that has change bars applied to the whole paragraph. </para>
输出应该是(这里 fo:block 节点来自 xsl 中的其他模板):
<fo:block>
<fo:change-bar-begin change-bar-style="solid"/>
This is a paragraph that has change bars applied to the whole paragraph.
<fo:change-bar-end/>
</fo:block>
我正在使用此代码,但在某些情况下,它会在外部级别添加节点,而在其他情况下,它会删除其他模板中定义的节点(例如 fo:block)。
<xsl:template match="*[@changeStatus='new' or @changeStatus='changed']">
<fo:change-bar-begin change-bar-style="solid"/>
<xsl:apply-templates select="node() | @*" />
<fo:change-bar-end/>
</xsl:template>
在这里,para 只是一个示例,我需要此代码来处理许多元素,因此不能选择使用 call-template。 请建议最好的方法。
【问题讨论】:
-
这并不容易,如果属性只是在其他内容之前添加内容,您可以将
<fo:block><xsl:apply-templates /></fo:block>更改为<fo:block><xsl:apply-templates select="@* | node()" /></fo:block>并添加匹配@changeStatus[. = ('new', 'changed')]的模板以输出例如<fo:change-bar-begin change-bar-style="solid"/>。但是,如果您需要让属性在不同位置创建结果元素,则需要apply-templates两次并使用不同的模式。