在 XSLT 处理期间是否可以导航到匹配节点的父节点? [英] Is it possible to navigate to the parent node of a matched node during XSLT processing?

查看:50
本文介绍了在 XSLT 处理期间是否可以导航到匹配节点的父节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 OpenXML 文档,使用一些 XSLT 处理主要文档部分.

I'm working with an OpenXML document, processing the main document part with some XSLT.

我通过

<xsl:template match="w:sdt">
</xsl:template>

在大多数情况下,我只需要用其他东西替换匹配的节点,就可以了.

In most cases, I simply need to replace that matched node with something else, and that works fine.

但是,在某些情况下,我需要替换的不是匹配的 w:sdt 节点,而是最近的 w:p 祖先节点(即包含 sdt 节点的第一个段落节点).

BUT, in some cases, I need to replace not the w:sdt node that matched, but the closest w:p ancestor node (ie the first paragraph node that contains the sdt node).

诀窍是用于决定一个或另一个的条件是基于从 sdt 节点的属性派生的数据,所以我不能使用典型的 xslt xpath 过滤器.

The trick is that the condition used to decide one or the other is based on data derived from the attributes of the sdt node, so I can't use a typical xslt xpath filter.

我正在尝试做这样的事情

I'm trying to do something like this

<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 节点,而不是它的父节点.

This whole concept works, BUT no matter what I've tried, It always applies the formatting from the CUSTOM FORMAT template to the w:p node, NOT it's parent node.

这几乎就像您无法从匹配节点中引用父节点一样.也许你不能,但我还没有找到任何文件说你不能

It's almost as if you can't reference a parent from a matching node. And maybe you can't, but I haven't found any docs that say you can't

有什么想法吗?

推荐答案

这个:

<xsl:apply-templates select="(ancestor::w:p)/.." mode="backout" />

将查找作为上下文节点祖先的所有 w:p 元素,并将模板应用于每个元素的父元素.在我看来,您可能只想找到最近的祖先,例如:

will find all w:p elements that are ancestors of the context node, and apply templates to the parent elements of each. It sounds to me like maybe what you want to do is find only the nearest ancestor, e.g.:

<xsl:apply-templates select="ancestor::w:p[1]/.." mode="backout" />

但是你在这里描述的应该是有效的,以某种方式.您可能应该验证您认为正在发生的事情实际上是正在发生的事情,方法是将您的 backout 模板替换为更具诊断性的内容,例如:

But what you're describing here should be working, in some fashion. You should probably verify that what you think is happening is actually what's happening, by replacing your backout template with something more diagnostic, e.g.:

<xsl:template match="node()" mode="backout">
   <xsl:text>backout matched a </xsl:text>
   <xsl:value-of select="name()"/>
   <xsl:text> element.</xsl:text>
</xsl:template>

这篇关于在 XSLT 处理期间是否可以导航到匹配节点的父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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