开发者

XSLT 1.0 Regular Expression in for loop

开发者 https://www.devze.com 2023-04-09 05:44 出处:网络
So I have xml like <depot id=\"D7\"> <address> <number>6000</number> <street>Yonge</street>

So I have xml like

<depot id="D7">
    <address> 
        <number>6000</number> 
        <street>Yonge</street> 
        <city>Toronto</city> 
        <province>ON&开发者_运维技巧lt;/province> 
        <postal_code>M2M 2E4</postal_code> 
    </address> 
</depot> 

I have hundreds of there depot in xml

now in my xsl, I have defined a variable called 'locale' that stores a postal code like "M1C". After this I want to select only those depot where the postal_code is like 'locale'. In other words, If I specify locale to be "M1C", then I should get all the depot whose postal_code contains "M1C", so depot with "M1C A18", "M1C B2C", etc all should be in the result.

Currently I have the line below

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

which gives me only depot with exact postal code match and not the ones with "M1C A18", "M1C B2C", etc. I want to use something like

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

with wildcards but it does not works. Suggestions?


Use:

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

Here we assume that any depot has a single address/postal_code descendent and that no possible value of $locale is a prefix of any other possible value of $locale.

If, for example, the second assumption isn't true, then use:

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

True regular expression capabilities are available in XPath 2.0 (such as the matches() function), but they aren't necessary for a simple problem as this one.


Use this:

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

to match only depot elements that contain the fragment defined in $locale.

0

精彩评论

暂无评论...
验证码 换一张
取 消