XSLT2:如何在 XPath2 谓词中引用有关当前节点的属性 [英] XSLT2: How to reference attributes about the current node in XPath2 predicates

查看:25
本文介绍了XSLT2:如何在 XPath2 谓词中引用有关当前节点的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了 另一个问题 将此作为它的一个方面.我被告知要澄清这个问题,但那个问题已经很长很复杂,所以我创建了一个新问题.

I had posted another question with this as one aspect of it. I was told to clarify the question, but that question was already pretty long and complicated, so I created a new one.

我想知道在 XPath 表达式测试中是否有引用 current 节点属性的标准方法.

I want to know if there is standard way of referencing the current node's attribute in an XPath expression testing for another one.

例如,考虑以下 XSLT

For an example, consider the following XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:for-each select="potato/stem[eye]">
        In session <xsl:value-of select="@sessionID"/>, the potato had <xsl:value-of select="/potato/stem[@sessionID=@sessionID][scc]/scc/@leafnumber"/> s.c.c. leaves.
    </xsl:for-each>
</xsl:template>

(此问题底部的 XML 源.)(请注意,for-each 引用了 stem[eye] 类型的节点,但第二个值请求引用了 stem[scc] 类型的节点,这些节点位于源 XML 树的不同分支上.)

(XML source at bottom of this question.) (Note that the for-each references nodes of type stem[eye] but the second value requested references nodes of type stem[scc], which are on a different branch of the source XML tree.)

现在,很明显,其中的@sessionID=@sessoinID"部分大部分是没有意义的,因为 XPath 认为这是节点的 sessionID 属性的值应该等于......节点的 sessionID 属性的值."

Now, obviously, the "@sessionID=@sessoinID" part of that is mostly meaningless because XPath perceives this as "The value of the sessionID attribute of the node should equal ... the value of the sessionID attribute of the node."

但我想说的是测试以确保该节点的 seesionID 属性值(XPath 表达式中的那个)与任何/stem[eye] 节点的 sessionID 相同我我现在在."

But what I want to say is "Test to make sure the value of the seesionID attribute of that node (the one in the XPath expression) is the same as the sessionID of the node of whatever /stem[eye] node I'm in right now."

我不能用变量来做到这一点,因为不允许在 for-each 子句中声明变量.

I can't do this with a variable, because you are not allowed to declare a variable within a for-each clause.

作为参考,这是 XML 源代码.它的结构不是人们想要的,但它是我必须使用的.

For reference, this is the XML source. Its structure is not what one would like, but it is what I have to work with.

<?xml version="1.0" encoding="utf-8"?>
<potato>
<stem sessionID="1">
    <eye number = "25"/>
</stem>
<stem sessionID="3">
    <eye number = "33"/>
</stem>

<stem sessionID="1">
    <scc leafnumber = "234" />
</stem>
<stem sessionID="2">
    <scc leafnumber = "433"/>
</stem>
<stem sessionID="3">
    <scc leafnumber = "463"/>
</stem>

<stem sessionID="1">
    <agd leafnumber = "154"/>
</stem>
<stem sessionID="2">
    <agd leafnumber = "233"/>
</stem>
<stem sessionID="3">
    <agd leafnumber = "113"/>
</stem> 
</potato>

我正在寻找的输出是:

在会话 1 中,马铃薯有 234 个 s.c.c.叶子.

In session 1, the potato had 234 s.c.c. leaves.

在第 3 节中,马铃薯的 463 s.c.c.叶子.

In session 3, the potato had 463 s.c.c. leaves.

(当然,这只是沙盒示例.我意识到可能有一些简单的方法可以以完全不同的方式完成上述输出,但我希望这个示例能解决我的问题,即如何使用与用于 XPath 的谓词中的当前节点(例如,在 for-each 中)搜索不同的节点.)

(Of course, this is all just sandbox example. I realize there are probably easy ways to accomplish the above output in a completely different manner, but I hope this example gets across my question, which is how to use values associated with the current node (say, in a for-each) in a predicate for an XPath searching out a different one.)

推荐答案

在 XSLT 1.0 中,您可以使用标准函数 current()匹配当前模板或最里面的 xsl:for-each:

/potato/stem[@sessionID=current()/@sessionID][scc]/scc/@leafnumber

或通过定义一个键(在全局级别):

<xsl:key name="kPotById" match="stem[scc]" use="@sessionID"/>

并引用此键:

key('kPotById', @sessionID)/scc/@leafnumber

在 XSLT 2.0/XPath 2.0 中,您有其他表达方式(范围变量):

for $thisSessionID in @sessionId
 return
    /potato/stem[@sessionID=$thisSessionID][scc]/scc/@leafnumber

这篇关于XSLT2:如何在 XPath2 谓词中引用有关当前节点的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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