Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字符串xslt中查找数字_Xslt_Xslt 1.0 - Fatal编程技术网

在字符串xslt中查找数字

在字符串xslt中查找数字,xslt,xslt-1.0,Xslt,Xslt 1.0,我需要能够找到元素中某个数字的最后一次出现。 比如说, <DATA1>States45bx3.33</DATA1> <DATA2>States45bx33</DATA2> 状态45bx3.33 状态45bx33 从这个字符串中,我想得到XSLT中的最后一个数值,即第一种情况下的3.33,第二种情况下的33。 任何帮助都将不胜感激。如果您能够应用XSLT 2.0,请使用以下工具: <xsl:value-of select="replace(

我需要能够找到元素中某个数字的最后一次出现。 比如说,

<DATA1>States45bx3.33</DATA1>
<DATA2>States45bx33</DATA2>
状态45bx3.33
状态45bx33
从这个字符串中,我想得到XSLT中的最后一个数值,即第一种情况下的3.33,第二种情况下的33
任何帮助都将不胜感激。

如果您能够应用XSLT 2.0,请使用以下工具:

<xsl:value-of select="replace(., '.*[^.\d](\d*\.?\d+)$', '$1')"/>

这将在当前节点的末尾返回一个数字,可以是整数,也可以是浮点数

所用正则表达式的解释:
(1) .*=字符串开头的任意字符数
(2) [^.\d]=一个不是点也不是数字的字符
(3) (…)分组,允许按$1向后引用
(4) \d*?\d+=可选数字+可选点+数字
(5) $=字符串结尾
(6) $1请返回第1组

放入xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <output>
            <xsl:for-each select="root/*">
                <num>
                    <xsl:value-of select="replace(., '.*[^.\d](\d*\.?\d+)$', '$1')"/>
                </num>
            </xsl:for-each>
        </output>
    </xsl:template>

</xsl:stylesheet>

应用于输入

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <number>States45bx3.33</number>
    <number>States45bx33</number>
    <number>asdfs12310.13.0sads23.23</number>
</root>

状态45BX3.33
状态45bx33
asdfs12310.13.0sads23.23
这导致了

<?xml version="1.0" encoding="UTF-8"?>
<output>
    <num>3.33</num>
    <num>33</num>
    <num>23.23</num>
</output>

3.33
33
23.23
您可以将其放入xsl:function(该函数也需要XSLT 2.0),这样您就可以通过调用该函数在末尾检索数字,并在函数外部执行围绕结果的元素:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:custom="http://customFunctions">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <output>
            <xsl:for-each select="root/*">
                <num>
                    <xsl:value-of select="custom:getNumberAtEnd(.)"/>
                </num>
            </xsl:for-each>
        </output>
    </xsl:template>

    <xsl:function name="custom:getNumberAtEnd">
        <xsl:param name="input"/>
        <xsl:value-of select="replace($input, '.*[^.\d](\d*.?\d+)$', '$1')"/>
    </xsl:function>

</xsl:stylesheet>

备注:当字符串末尾没有数字时,函数调用的结果或更早的xsl:value语句将成为整个输入字符串。当您不想这样做时,您必须首先测试字符串,或者当您确定字符串的结尾总是一个格式良好的数字时,您可以用下面的一个替换使用的正则表达式,它只返回最后一个非点、非数字(可能是空字符串)后面的任何内容

<xsl:value-of select="replace($input, '.*[^.\d](.*)$', '$1')"/>

好的,这是XSLT1.0中的一个解决方案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <output>
            <xsl:for-each select="root/*">
                <xsl:element name="{name()}">
                    <xsl:call-template name="getEndNumber">
                        <xsl:with-param name="input" select="normalize-space(.)"/>
                        <xsl:with-param name="dotIncluded" select="true()"/>
                    </xsl:call-template>
                </xsl:element>
            </xsl:for-each>
        </output>
    </xsl:template>

    <xsl:template name="getEndNumber">
        <xsl:param name="input"/>
        <xsl:param name="dotIncluded"/>
        <xsl:variable name="len" select="string-length($input)"/>
        <xsl:choose>
            <xsl:when test="substring($input, $len, 1) &gt;= '0' and substring($input, $len, 1) &lt;= '9'">
                <xsl:call-template name="getEndNumber">
                    <xsl:with-param name="input" select="substring($input, 1, $len - 1)"/>
                    <xsl:with-param name="dotIncluded" select="$dotIncluded"/>
                </xsl:call-template>
                <xsl:value-of select="substring($input, $len, 1)"/>
            </xsl:when>
            <xsl:when test="substring($input, $len, 1) = '.' and $dotIncluded">
                <xsl:call-template name="getEndNumber">
                    <xsl:with-param name="input" select="substring($input, 1, $len - 1)"/>
                    <xsl:with-param name="dotIncluded" select="false()"/>
                </xsl:call-template>
                <xsl:value-of select="substring($input, $len, 1)"/>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

适用于

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>States45bx3.33</a>
    <b>States45bx33</b>
    <c>asdfs12310.13.0sads23.23</c>
    <d>asdfs12310.13.0sads23.23z</d>
    <e>asdfs12310.13.0sads23.23.34</e>
</root>

状态45BX3.33
状态45bx33
asdfs12310.13.0sads23.23
asdfs12310.13.0sads23.23z
asdfs12310.13.0sads23.23.34
结果是

<?xml version="1.0" encoding="UTF-8"?>
<output>
    <a>3.33</a>
    <b>33</b>
    <c>23.23</c>
    <d/>
    <e>23.34</e>
</output>

3.33
33
23.23
23.34

老实说,这有点难以实现。如果没有小数点,那就容易一点了。我不会只处理第二种情况的答案。文本格式是固定的吗?类似地,数字只会出现在
bx
之后??或者字符串可以是任何内容?XSLT2.0在其XPath中支持正则表达式。您有什么理由继续使用XSLT 1.0吗?@Krab:是的,使用XSLT 2.0确实更容易满足行业要求,但OP需要XSLT1的帮助。。看看你是否能帮助他。我试过了,但如果数字之间的字符串不能以匹配字符开头,它就不起作用了。@infantprogrammer'Aravind'完成!