xml 如何与xslt中的多个字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6357166/
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
How to compare against multiple strings in xslt
提问by Lamps
For comparing an xml string value against multiple strings, I am doing the following.
为了将 xml 字符串值与多个字符串进行比较,我正在执行以下操作。
<xsl:if test="/Lines/@name = 'John' or /Lines/@name = 'Steve' or /Lines/@name = 'Marc' " >
Can any one tell me, instead of using 'or' in the above case, how can I check whether a string is existing in an set of strings using xslt.
任何人都可以告诉我,在上述情况下,我如何不使用“或”来检查字符串是否存在于使用 xslt 的一组字符串中?
Thanks.
谢谢。
回答by Dimitre Novatchev
Three ways of doing this:
执行此操作的三种方法:
- Use a pipe (or other appropriate character) delimited string
- 使用管道(或其他适当的字符)分隔的字符串
...
...
<xsl:template match=
"Lines[contains('|John|Steve|Mark|',
concat('|', @name, '|')
)
]
">
<!-- Appropriate processing here -->
</xsl:template>
.2. Test against an externally passed parameter. If the parameter is not externally set, and we are using XSLT 1.0, the xxx:node-set()extension function needs to be used to convert it to normal node-set, before accessing its children
.2. 针对外部传递的参数进行测试。如果参数不是外部设置的,而我们使用的是 XSLT 1.0,则xxx:node-set()需要使用扩展函数将其转换为普通节点集,然后才能访问其子节点
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- externally-specified parameter -->
<xsl:param name="pNames">
<n>John</n>
<n>Steve</n>
<n>Mark</n>
</xsl:param>
<xsl:template match="Lines">
<xsl:if test="@name = $pNames/*">
<!-- Appropriate processing here -->
</xsl:if>
</xsl:template>
</xsl:stylesheet>
.3. In XSLT 2.0 compare against a sequence of strings
.3. 在 XSLT 2.0 中与字符串序列进行比较
<xsl:template match="Lines[@name=('John','Steve','Mark')]">
<!-- Appropriate processing here -->
</xsl:template>
回答by Martin Honnen
XSLT 2.0 only: <xsl:if test="/Lines/@name = ('John', 'Steve', 'Marc')">
仅 XSLT 2.0: <xsl:if test="/Lines/@name = ('John', 'Steve', 'Marc')">
With XSLT 1.0 you can't write a literal expression representing a sequence of strings or a set of strings but if you know the literal values then you can construct a set of nodes e.g.
使用 XSLT 1.0,您不能编写表示字符串序列或一组字符串的文字表达式,但如果您知道文字值,那么您可以构造一组节点,例如
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:data="http://example.com/data"
exclude-result-prefixes="data">
<data:data xmlns="">
<value>John</value>
<value>Steve</value>
<value>Marc</value>
</data:data>
<xsl:variable name="values" select="document('')/xsl:stylesheet/data:data/value"/>
<xsl:template match="...">
<xsl:if test="/Lines/@name = $values">..</xsl:if>
</xsl:template>
</xsl:stylesheet>
回答by MikeyKennethR
Yep - I use substring - put all your name in a string - xsl:variable - then if contains true, the name is there
是的 - 我使用子字符串 - 把你所有的名字放在一个字符串中 - xsl:variable - 然后如果包含真,名字就在那里
e.g.
例如
<xsl:variable name="months">**janfebmaraprmajjunjulaugsepoktnovdec</xsl:variable>
<xsl:if test="contains($months,'feb')"> do stuff ...
回答by rahulmohan
XPath has a some $x in (1,2,..) satisfies $x>10expression that could be useful for this. See: http://www.java2s.com/Code/XML/XSLT-stylesheet/everyandsomeoperator.htm
XPath 有一个some $x in (1,2,..) satisfies $x>10可能对此有用的表达式。请参阅:http: //www.java2s.com/Code/XML/XSLT-stylesheet/everyandsomeoperator.htm
回答by Sorrow
For space-separated words you can use index-of(tokenize("list of allowed", "\s+"), "needle"))or matchto go with regular expressions, although I am pretty sure there is something smarter than this.
对于空格分隔的单词,您可以使用index-of(tokenize("list of allowed", "\s+"), "needle"))或match使用正则表达式,尽管我很确定有比这更聪明的东西。
回答by Emiliano Poggi
Other possiblity:
其他可能性:
XPath 2.0 (XSLT 2.0)
XPath 2.0 (XSLT 2.0)
matches(/Lines/@name, 'John|Steve|Marc')
In XSLT 1.0you have the similar function matchesprovided by EXSLT.
在XSLT 1.0 中,您拥有EXSLTmatches提供的类似功能。
Notice
注意
This is not exactmatch against the string, but a regexmatch, which in your case seems appropriate anyway.
这不是与字符串的精确匹配,而是正则表达式匹配,无论如何这在您的情况下似乎是合适的。

