如何与xslt中的多个字符串进行比较 [英] How to compare against multiple strings in xslt
本文介绍了如何与xslt中的多个字符串进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了将 xml 字符串值与多个字符串进行比较,我正在执行以下操作.
For comparing an xml string value against multiple strings, I am doing the following.
<xsl:if test="/Lines/@name = 'John' or /Lines/@name = 'Steve' or /Lines/@name = 'Marc' " >
谁能告诉我,在上述情况下,我如何使用 xslt 来检查字符串是否存在于一组字符串中,而不是使用或".
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.
谢谢.
推荐答案
三种方法:
- 使用竖线(或其他适当的字符)分隔的字符串
...
<xsl:template match=
"Lines[contains('|John|Steve|Mark|',
concat('|', @name, '|')
)
]
">
<!-- Appropriate processing here -->
</xsl:template>
.2.针对外部传递的参数进行测试.如果参数不是外部设置的,而我们使用的是 XSLT 1.0,则需要使用 xxx:node-set()
扩展函数将其转换为普通节点集,然后才能访问其子节点
.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
<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.在 XSLT 2.0 中与字符串序列进行比较
<xsl:template match="Lines[@name=('John','Steve','Mark')]">
<!-- Appropriate processing here -->
</xsl:template>
这篇关于如何与xslt中的多个字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文