将谓词应用于当前节点 [英] Apply a predicate to the current node

查看:100
本文介绍了将谓词应用于当前节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我要发布此问题的答案,因为通常针对该问题提供的解决方案是不必要的冗长,并且我想将其记录下来.我找不到与此相关的现有SO问题,但是如果有的话,请将其作为副本关闭.)

我正在寻找一种执行XPath选择的方法,以仅在满足特定条件时选择当前节点.例如,当我想有条件地将XSLT模板应用于当前节点时,这将非常有用:

I am looking for a way to perform an XPath selection to select the current node only if it matches a certain condition. This would be useful, for example, when I want to conditionally apply an XSLT template to the current node:

<xsl:template match="Device">
  <div>
    <h2><xsl:value-of select="Name" /></h2>
    <xsl:apply-templates select="???[Featured = 'true']" mode="featured" />
    <p><xsl:value-of select="Description" /></p>
  </div>
</xsl:template>

<xsl:template match="Book">
  <div>
    <h2><xsl:value-of select="Title" /></h2>
    <xsl:apply-templates select="???[FeaturedBook = 'true']" mode="featured" />
    <h3><xsl:value-of select="Author" /></h3>
    <p><xsl:value-of select="Summary" /></p>
  </div>
</xsl:template>

<xsl:template match="node()" mode="featured">
  <p class='featured-notice'>This is a featured item!
    <a href="/AddToCart?productId={Id}">Buy now</a> to get a 15% discount.
  </p>
</xsl:template>

我尝试使用.[Featured = 'true'],但是出现语法错误.我该怎么办?

I have tried using .[Featured = 'true'], but I get a syntax error. How can I do this?

我不会在此处添加输入和输出,因为它们与问题相切,并且会导致问题过长,但是如果您想了解我的想法,可以将它们放在此处: 输入输出.

推荐答案

由于语法规则,XPath 1.0中不允许使用语法.[predicate](有关详细信息,请参阅本文的结尾).

The syntax .[predicate] is not allowed in XPath 1.0 on account of the syntax rules (see the end of this post for the gritty details).

100%的建议说,唯一的选择就是为此使用self::node():

100% of the advice I have found says that the only option is to use self::node() for this:

self::node()[Featured = 'true']

此XPath测试器甚至旨在告诉用户使用self::node()[predicate]尝试使用.[predicate],但这不是唯一的选择.

This XPath tester is even specifically designed to tell users to use self::node()[predicate] if they try to use .[predicate], but this is not the only option.

一个有效且更简洁的选择是将缩写的步骤用括号括起来:

A valid and more concise option is to just wrap the abbreviated step in parentheses:

(.)[Featured = 'true']

根据XPath 1.0语法规则,这是完全有效的(而且我认为更清楚).

This is perfectly valid by XPath 1.0 syntax rules (and in my opinion, a lot clearer).

您也可以在..的缩写步骤中使用此方法,甚至可以将其升级到多个级别:

You can also use this approach with the .. abbreviated step, even going up multiple levels:

Select grandfather node if it is featured


../..[Featured = 'true']                - Not valid

../../../*[Featured = 'true']           - Valid, but not accurate

../../self::node()[Featured = 'true']   - Valid, but verbose

(../..)[Featured = 'true']              - Valid


附录:为什么无法在XPath 1.0中使用.[predicate]


Addendum: Why it's not possible to use .[predicate] in XPath 1.0

以下是XPath 1.0中步骤"的定义(基本上,用斜杠分隔的XPath节点选择表达式的各个部分称为步骤"):

The following is the definition of a "step" in XPath 1.0 (basically, the pieces of an XPath node selection expression separated by slashes are called "steps"):

[4]步骤:: = AxisSpecifier NodeTest谓词* |缩写步骤

[4] Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep

这意味着一个步骤包含两个可能选项之一:

This means that one step consists of one of two possible options:

  • 一个轴说明符(可以是一个空字符串),然后是节点测试,然后是0个或多个谓词
  • 一个简短的步骤:...

没有选择缩写词后跟谓词的选择.

There is no option to have an abbreviated step followed by predicates.

这篇关于将谓词应用于当前节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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