Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Regex XSLT-获取text()节点的数字值_Regex_Xslt_Xslt 2.0 - Fatal编程技术网

Regex XSLT-获取text()节点的数字值

Regex XSLT-获取text()节点的数字值,regex,xslt,xslt-2.0,Regex,Xslt,Xslt 2.0,我有如下XML文件: <sec> <para>Section 1- TOC</para> <para>Section 4* Main</para> <para>Section 3$ Basic content</para> <para>Section 11_ Section 10</para> <para>Section 15@ Appe

我有如下XML文件:

<sec>
    <para>Section 1- TOC</para>
    <para>Section 4* Main</para>
    <para>Section 3$ Basic content</para>
    <para>Section 11_ Section 10</para>
    <para>Section 15@ Appendix 6</para>
</sec>
此示例返回数字值之前的子字符串,但我需要在“Section”字符串之后获取数字值。。(输出值应为1,4,3,11和15)

我尝试了一些内置函数(前字符串、后字符串、匹配项…),但找不到合适的解决方案


有人能给我推荐一种获取该数值的方法吗?

您可以使用
分析字符串
,正如注释中所建议的那样,请参阅以获取一个工作示例,该示例可以

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:abc="http://example.com/abc"
    exclude-result-prefixes="xs abc">

    <xsl:function name="abc:get-section-number" as="xs:integer">
        <xsl:param name="string" as="xs:string"/>
        <xsl:analyze-string select="$string" regex="^Section\s+([0-9]+)">
            <xsl:matching-substring>
                <xsl:sequence select="xs:integer(regex-group(1))"/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:function>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="para">
        <xsl:copy>
            <xsl:value-of select="abc:get-section-number(.)"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

检查。您似乎只需要
节\s+(\d+)
。然后
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:abc="http://example.com/abc"
    exclude-result-prefixes="xs abc">

    <xsl:function name="abc:get-section-number" as="xs:integer">
        <xsl:param name="string" as="xs:string"/>
        <xsl:analyze-string select="$string" regex="^Section\s+([0-9]+)">
            <xsl:matching-substring>
                <xsl:sequence select="xs:integer(regex-group(1))"/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:function>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="para">
        <xsl:copy>
            <xsl:value-of select="abc:get-section-number(.)"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>