xml XSLT:如何在“匹配”属性中表示 OR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/616427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT: How to represent OR in a "match" attribute?
提问by tomato
I want to perform a series of operations on elements that matched the name "A" or "B". I'm thinking of something like this below, but it doesn't work.
我想对匹配名称“A”或“B”的元素执行一系列操作。我正在考虑下面这样的事情,但它不起作用。
<xsl:template match= " 'A' or 'B'" >
<!-- whatever I want to do here -->
</xsl:template>
Couldn't find the appropriate XSLT language reference for it. Please help! Thanks!!
找不到合适的 XSLT 语言参考。请帮忙!谢谢!!
回答by David
回答by Robert Rossney
Generally A | Bis the right way to do this. But the pipe character is basically a union of two complete XPath expressions. It can be annoying to use it in a case like this:
通常A | B是执行此操作的正确方法。但是管道字符基本上是两个完整 XPath 表达式的联合。在这样的情况下使用它可能很烦人:
/red/yellow/blue/green/gold | red/orange/blue/green/gold
since you're repeating the entirety of the expression except for the one small piece of it's that changing.
因为你在重复整个表达式,除了一小部分正在改变。
In cases like this, it often makes sense to use a predicate and the name()function instead:
在这种情况下,使用谓词和name()函数通常是有意义的:
/red/*[name() = 'yellow' or name()='orange']/blue/green/gold
This technique gives you access to a much broader range of logical operations. It's also (conceivably) faster, as the XPath navigator only has to traverse the nodes it's testing once.
这种技术使您可以访问范围更广的逻辑操作。它也(可以想象)更快,因为 XPath 导航器只需遍历它正在测试的节点一次。
回答by tomato
I think it's more convenient to use this XPath
我觉得用这个XPath比较方便
/red/(yellow | orange)/blue/green/gold
rather than
而不是
/red/*[name() = 'yellow' or name()='orange']/blue/green/gold
回答by Dimitre Novatchev
<xsl:template match= " 'A' or 'B'" >
<xsl:template match= " 'A' or 'B'" >
There are a few problems with this match pattern:
这种匹配模式存在一些问题:
A template matches nodes, not strings. Therefore, the names of the elements to be matched should not be specified as quoted strings.
The XPath operator "or"acts on two boolean values, not on nodes. What is necessary here is another XPath operator -- the unionoperator "|".
模板匹配节点,而不是字符串。因此,要匹配的元素的名称不应指定为带引号的字符串。
该XPath的运算符“或”作用于两个布尔值,而不是节点。这里需要的是另一个 XPath 运算符——联合运算符“|” .
Taking into account the above, one will correctly specify the template rule as:
考虑到上述情况,可以将模板规则正确指定为:
<xsl:template match= "A | B" >
<!-- whatever I want to do here -->
</xsl:template>
回答by qxotk
The below information was gleaned from: http://www.cafeconleche.org/books/bible2/chapters/ch17.html#d1e2090
以下信息来自:http: //www.cafeconleche.org/books/bible2/chapters/ch17.html#d1e2090
I will paraphrase, please search for the text "Using the or operator |" in that document.
我来解释一下,请搜索文本“使用或运算符 |” 在那个文件中。
The syntax is:
语法是:
<xsl:template match="A|B">
<!-- Do your stuff> -->
</xsl:template>

