XSLT 和Xpath语法>如何引用“外部”中的元素范围

发布于 2024-08-13 00:54:10 字数 459 浏览 7 评论 0原文

我的以下工作100%正确。 然而,为了满足我的好奇心......有没有办法在不声明 currentID 变量的情况下实现相同的目的? 有没有办法从 Xpath“测试”条件中引用它?

条件中的xpath查询必须引用2个@id属性来查看它们是否匹配。

  • “当前”@id
  • 每个“祖先”@id

这是代码:

<xsl:variable name="currentID" select="@id" />
<xsl:attribute name="class">
<xsl:if test="count($currentPage/ancestor::node [@id = $currentID])&gt;0">descendant-selected </xsl:if>
</xsl:attribute>

I have the following working 100% correctly.
However to satisfy my curiosity... is there a way to achieve the same without declaring the currentID variable?
Is there some way to reference it from within the Xpath "test" condition?

The xpath query in the condition must refer to 2 @id attributes to see if they match.

  • the 'current' @id
  • each 'ancestor' @id

Here's the code:

<xsl:variable name="currentID" select="@id" />
<xsl:attribute name="class">
<xsl:if test="count($currentPage/ancestor::node [@id = $currentID])>0">descendant-selected </xsl:if>
</xsl:attribute>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

停滞 2024-08-20 00:54:10

由于您从上下文节点中选择了 $currentID

<xsl:variable name="currentID" select="@id" />

您可以使用 current() 函数,该函数始终引用 XSLT 上下文节点:

<xsl:attribute name="class">
  <xsl:if test="count($currentPage/ancestor::node[@id = current()/@id]) > 0]">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

这样您就不需要一个变量。

其他一些注意事项:

  • 我建议使用 如上所示。这使您可以更自由地格式化代码并避免过长的行。
  • 您不需要执行 count() > 0,只需选择节点就足够了。如果不存在,则返回空节点集。它始终评估为 false,而非空节点集始终评估为 true。

如果您在 XSL 样式表中定期通过 @id 引用节点,则 将变得有益:

<xsl:key name="kNodeById" match="node" use="@id" />

<!-- ... -->

<xsl:attribute name="class">
  <xsl:if test="key('kNodeById', @id)">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

上面不需要 current() 因为在 XPath 谓词之外,上下文没有改变。另外,我不会 count() 节点,因为这是多余的(如所解释的)。

Since you select the $currentID from the context node:

<xsl:variable name="currentID" select="@id" />

you can use the current() function, which always refers to the XSLT context node:

<xsl:attribute name="class">
  <xsl:if test="count($currentPage/ancestor::node[@id = current()/@id]) > 0]">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

This way you don't need a variable.

A few other notes:

  • I recommend using <xsl:text> like shown above. This gives you more freedom to format your code and avoid overly long lines.
  • You don't need to do a count() > 0, simply selecting the nodes is sufficient. If none exist, the empty node-set is returned. It always evaluates to false, while non-empty node-sets always evaluate to true.

If you refer to nodes by @id regularly in your XSL stylesheet, an <xsl:key> would become beneficial:

<xsl:key name="kNodeById" match="node" use="@id" />

<!-- ... -->

<xsl:attribute name="class">
  <xsl:if test="key('kNodeById', @id)">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

The above does not need current() since outside of an XPath predicate, the context is unchanged. Also, I don't count() the nodes, since this is redundant (as explained).

清风无影 2024-08-20 00:54:10

使用 current() 来引用模板处理的当前节点:

<xsl:if test="count($currentPage/ancestor::node [@id = current()/@id])>0">

Use current() to refer to the current node processed by the template:

<xsl:if test="count($currentPage/ancestor::node [@id = current()/@id])>0">
一个人的旅程 2024-08-20 00:54:10

蒂姆让我思考......
我认为我把事情复杂化了,我尝试了以下有效的方法。

<xsl:if test="@id = $currentPage/ancestor::node/@id">descendant-selected </xsl:if>

XSLT 似乎很乐意将一个属性与一组属性进行比较,并评估其中任何一个匹配是否为 true?如果有人对为什么这样做有更好的解释或更好(更简洁)的东西,那么就把它写下来。

Tim got me thinking....
I think I was over complicating things, and I tried the following which works.

<xsl:if test="@id = $currentPage/ancestor::node/@id">descendant-selected </xsl:if>

XSLT seems happy comparing an attribute with a selection of attributes, and evaluating true if any of them match? if anyone has a better explanation of why this works or something better (more succinct) then put it down.

兔姬 2024-08-20 00:54:10

正如已经清楚的那样,引用“外部范围”不是问题,因为您可以使用“=”运算符进行直接比较。然而,在某些情况下,您确实需要 current() 以及更多,甚至 current() 也无法削减它(因为您需要在两个以上的上下文之间“加入”)。在这些情况下,XPath 2.0 的“for”表达式是不可或缺的。

As it has already become clear, referring to the "outer scope" was not an issue, since you could do a direct comparison using the "=" operator. However, there are some cases where you do need current() and more besides where even current() doesn't cut it (because you need to "join" between more than just two contexts). In those cases, XPath 2.0's "for" expressions are indispensable.

南城追梦 2024-08-20 00:54:10

你可以简单地这样做:

<xsl:if test="count($currentPage[ancestor::node/@id = @id])>0">descendant-selected </xsl:if>

You could simply do:

<xsl:if test="count($currentPage[ancestor::node/@id = @id])>0">descendant-selected </xsl:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文