2013-04-08 33 views
1

我已经定义的变量称为标记,其选择的节点集的名字有几个值中的一个元素:如何缩短此xpath表达式?

<xsl:variable name="tags" select="//xs:element[@name='Subscription' or @name='Account' or @name='Product' or @name='ProductRatePlan' or @name='ProductRatePlanCharge' or @name='Usage']"/> 

我想定义一个变量,例如:

<xsl:variable name="tagNames" select="'Subscription','Account','Product','ProductRatePlan','ProductRatePlanCharge','Usage'/> 

怎么可能我重写第一个表达式来选择所有名称在集合$ tagNames中的节点?从本质上说,我要找的是很是类似一个SQL集合成员的操作:

SELECT * FROM tags WHERE name in ('Subscription', 'Account', 'Product'....) 
+0

你使用哪种版本的XSLT的? – nwellnhof 2013-04-08 20:05:10

+0

我相信它是2.0。 – 2013-04-09 20:14:38

回答

2

在XPath 2.0你可以写:

<xsl:variable name="tags" select="//xs:element[@name=('Subscription','Account','Product','ProductRatePlan','ProductRatePlanCharge','Usage')]"/> 
+0

在XPath 1.0中,您可以执行类似''。 – 2013-04-08 22:59:57

1

一种方式做到这一点

//xs:element 
    [contains('|Subscription|Account|Product|ProductRatePlan|ProductRatePlanCharge|Usage|', 
      concat('|', @name, '|'))] 

所以,你可以有一个变量或参数

<xsl:variable name="vTags" 
select="'|Subscription|Account|Product|ProductRatePlan|ProductRatePlanCharge|Usage|'"/> 

和你的主XPath表达式变成

//xs:element[contains($vTags, concat('|', @name, '|'))]