选择数字前面的单词

时间:2014-05-20 06:42:36

标签: regex xslt

我提供了以下XML数据。

<para>The functions and duties of the CCS set out in s 6 CA are focused on promoting efficient market conduct and competitiveness of markets in Singapore. Consumer welfare, mentioned in the Singapore&#8211;US FTA, is not expressly mentioned as a purpose of the CA, nor is it expressly set out in the CA as an objective to be safeguarded by the CCS. However, CCS Guideline 1, at para 2.1 on &#8220;Purpose&#8221; of the CA makes reference to how consumers benefit as a consequence of competition. And this is equal to 2.1% of the entire data</para></footnote></para></item>

在这里,我想要捕获单词paraparasparas 1.2 and 0.8后面的数字,但如果数据类似于general 1.2 and 1.3

则不应该捕获

我使用的正则表达式如下。

    <xsl:template match="text()">

    <xsl:analyze-string select="." regex="(([Cc]hapter)\s(\d+))">

        <xsl:matching-substring>
            <xsl:choose>
                <xsl:when test="number(regex-group(3)) &lt; number(9)">
                    <a href="{concat('er:#BGCL_CH_',format-number(number(regex-group(3)),'00'),'/','BGCL_CH_',format-number(number(regex-group(3)),'00'))}">
                        <xsl:value-of select="."/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>

        </xsl:matching-substring>

        <xsl:non-matching-substring>
            <xsl:analyze-string select="." regex="([0-9]+)\.([0-9]+)">

                  <xsl:matching-substring>
         <xsl:choose>
                                                                <xsl:when test="number(regex-group(1)) &lt; number(9)"> 
                                                                <a
          href="{concat('er:#CLI_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'0'),'-',format-number(number(regex-group(2)),'000'))}">
          <xsl:value-of select="."/>
        </a>
        </xsl:when>
        <xsl:otherwise>


                                                                                                             

                    <xsl:analyze-string select="."  regex="http://[^ ]+">
                        <xsl:matching-substring>
                            <a href="{.}">
                                <xsl:value-of select="."/>
                            </a>

                        </xsl:matching-substring>
                        <xsl:non-matching-substring>

                            <xsl:value-of select="."/>
                        </xsl:non-matching-substring>
                    </xsl:analyze-string>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

但是这里它以x.y格式捕获任何数字,我想只捕获文本是否采用以下格式

para x.y
paras x.y and x.y

我希望将x.y转换为以下格式

er:#BGCL_CH_x/Px-y

请让我知道我该怎么做。

由于

1 个答案:

答案 0 :(得分:1)

尝试类似:

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

    <xsl:template match="text()">
        <xsl:analyze-string select="." regex="paras\s([0-9]+)\.([0-9]+)\sand\s([0-9]+)\.([0-9]+)">
            <xsl:matching-substring>
                <xsl:choose>
                    <xsl:when test="number(regex-group(1)) &lt; number(9)">
                        <a
                            href="{concat('er:#CLI_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'0'),'-',format-number(number(regex-group(2)),'000'))}">
                            <xsl:value-of select="substring-before(., ' and')"/>
                        </a>
                        <xsl:text> and </xsl:text>
                        <a
                            href="{concat('er:#CLI_CH_',format-number(number(regex-group(3)),'00'),'/P',format-number(number(regex-group(3)),'0'),'-',format-number(number(regex-group(4)),'000'))}">
                            <xsl:value-of select="substring-after(., 'and ')"/>
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:analyze-string select="." regex="para\s([0-9]+)\.([0-9]+)">
                    <xsl:matching-substring>
                        <xsl:choose>
                            <xsl:when test="number(regex-group(1)) &lt; number(9)">
                                <a
                                    href="{concat('er:#CLI_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'0'),'-',format-number(number(regex-group(2)),'000'))}">
                                    <xsl:value-of select="."></xsl:value-of>
                                </a>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="."></xsl:value-of>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                        <xsl:value-of select="."/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>

</xsl:stylesheet>