取 xslt 中最后一个旁边的值 [英] Take the value next to the last in xslt

查看:26
本文介绍了取 xslt 中最后一个旁边的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Address>
1234Road
Unit 5 Lane
Town, City
SO1D 23Z
Customer No. 12321312312
</Address>

<Address>
21321311234Road
1234Road
Unit 5 Lane
Town, City
SO1D 23Z
Customer No. 12321312312
</Address>

谁能帮我获取始终位于客户编号之前的邮政编码的值?

Can anyone help me to take always the value of the postcode which will be always before Customer No.?

<xsl:value-of select="substring-before(substring-after(substring-after(substring-after(Address,'&#10;'),'&#10;'),'&#10;'),'&#10;')"/>

我已经使用了上面的但不打算在第二个例子中工作.需要找到一种方法来接下最后一个.

I have used the above but is not going to work on the second example. Need to find a way to take next to the last.

注意:每行之间有换行符(CRLF).

Note: there is Line breaks (CRLF) between each line.

非常感谢任何帮助

推荐答案

一种可能的方法是创建一个递归的substring-after-last"命名模板,并使用它传入出现在Customer No."之前的子字符串.

One possible way is to create a recursive "substring-after-last" named template, and use that passing in the substring that occurs before "Customer No. "

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" indent="yes" />

    <xsl:template match="Address">
        <xsl:call-template name="substring-after-last">
            <xsl:with-param name="text" select="$exCust" />
            <xsl:with-param name="arg" select="'&#10;'" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="substring-after-last">
        <xsl:param name="text" />
        <xsl:param name="arg" />

        <xsl:variable name="after" select="substring-after($text, $arg)" />
        <xsl:choose> 
            <xsl:when test="contains($after, $arg)">
                <xsl:call-template name="substring-after-last">
                    <xsl:with-param name="text" select="$after" />
                    <xsl:with-param name="arg" select="$arg" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$after" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

如果您只处理长度为 6 到 8 个字符(包括空格)的英国邮政编码,您可以通过一种非常粗略的方法来处理....

If you are only dealing with UK postcodes, which vary between 6 and 8 characters in length (including a space), you could do it via a very crude method....

<xsl:variable name="exCust" select="substring-before(., '&#10;Customer No. ')" />
<xsl:value-of select="substring-after(substring($exCust, string-length($exCust) - 8, 9), '&#10;')" />

这有点小技巧,如果前面的城镇/城市的长度是一个字符,也会失败!

This is a bit of a hack though, and will also fail if the preceding town/city is one character in length!

这篇关于取 xslt 中最后一个旁边的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆