for循环中的XSLT 1.0正则表达式

时间:2011-09-30 05:12:04

标签: regex xslt

所以我有xml喜欢

<depot id="D7">
    <address> 
        <number>6000</number> 
        <street>Yonge</street> 
        <city>Toronto</city> 
        <province>ON</province> 
        <postal_code>M2M 2E4</postal_code> 
    </address> 
</depot> 

我在xml中有数百个存储库

现在在我的xsl中,我定义了一个名为'locale'的变量,它存储了一个像“M1C”这样的邮政编码。 在此之后,我想只选择postal_code就像'locale'的那些仓库。换句话说,如果我将locale指定为“M1C”,那么我应该获得其postal_code包含“M1C”的所有软件仓库,所以具有“M1C A18”,“M1C B2C”等的软件仓库都应该在结果中。< / p>

目前我有以下一行

< xsl:for-each select="depot[address[postal_code=$locale]]">

这给了我唯一的邮政编码匹配,而不是“M1C A18”,“M1C B2C”等。我想使用像

这样的东西
<xsl:for-each select="depot[address[postal_code=*$locale*]]">

使用通配符,但不起作用。建议?

2 个答案:

答案 0 :(得分:1)

使用

depot[starts-with(address/postal_code, $locale)]

我们假设任何depot都有一个address/postal_code后代,并且$locale的任何可能值都不是$locale的任何其他可能值的前缀。

例如,如果第二个假设不成立,则使用

depot[starts-with(address/postal_code, concat($locale, ' '))]

XPath 2.0中提供了真正的正则表达式功能(例如matches()函数),但它们对于像这样的简单问题不是必需的。

答案 1 :(得分:1)

使用此:

<xsl:for-each select="depot[contains(address/postal_code,$locale)]" />

仅匹配包含depot中定义的片段的$locale个元素。