【发布时间】:2010-04-01 20:58:18
【问题描述】:
我正在处理一个 OpenXML 文档,使用一些 XSLT 处理主文档部分。
我通过
选择了一组节点<xsl:template match="w:sdt">
</xsl:template>
在大多数情况下,我只需将匹配的节点替换为其他内容即可。
但是,在某些情况下,我需要替换的不是匹配的 w:sdt 节点,而是最接近的 w:p 祖先节点(即包含 sdt 节点的第一个段落节点)。
诀窍在于,用于决定一个或另一个的条件是基于从 sdt 节点的属性派生的数据,所以我不能使用典型的 xslt xpath 过滤器。
我正在尝试做这样的事情
<xsl:template match="w:sdt">
<xsl:choose>
<xsl:when test={first condition}>
{apply whatever templating is necessary}
</xsl:when>
<xsl:when test={exception condition}>
<!-- select the parent of the ancestor w:p nodes and apply the appropriate templates -->
<xsl:apply-templates select="(ancestor::w:p)/.." mode="backout" />
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- by using "mode", only this template will be applied to those matching nodes
from the apply-templates above -->
<xsl:template match="node()" mode="backout">
{CUSTOM FORMAT the node appropriately}
</xsl:template>
这整个概念有效,但无论我尝试了什么,它总是将 CUSTOM FORMAT 模板中的格式应用于 w:p 节点,而不是它的父节点。
这几乎就像您无法从匹配节点引用父级一样。也许你不能,但我没有找到任何文档说你不能
有什么想法吗?
【问题讨论】: