正则表达式包含错误的输出

时间:2016-08-04 10:50:24

标签: xslt

我有以下XML代码。

 <pgs>IVA.004, 168IA.002</pgs>

我正在尝试在.之前获取数据并使用正则表达式编写匹配项。下面是我的XSLT。

<xsl:template match="pgs">
        <td class="pgs">
            <xsl:analyze-string select="." regex="([\w]+)\.([\w]+)">
                <xsl:matching-substring>
                    <xsl:variable name="nonStringFirstNum" select="number(translate(regex-group(1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',''))"/>
                    <xsl:variable name="nonStringSecondNum" select="number(translate(regex-group(3),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',''))"/>
                    <xsl:variable name="refVar">
                        <xsl:choose>
                            <xsl:when test="matches(regex-group(1),'XV') or matches(regex-group(1),'Sch') or ($nonStringFirstNum > 367)">
                                <xsl:value-of select="concat('er:#Link_PT_15/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'XIII') or ($nonStringFirstNum > 348 and $nonStringFirstNum &lt; 361)">
                                <xsl:value-of select="concat('er:#Link_PT_13/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'XII') or ($nonStringFirstNum > 341 and $nonStringFirstNum &lt; 344)">
                                <xsl:value-of select="concat('er:#Link_PT_12/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'X') or ($nonStringFirstNum > 325 and $nonStringFirstNum &lt; 332)">
                                <xsl:value-of select="concat('er:#Link_PT_10/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'IX') or ($nonStringFirstNum > 323 and $nonStringFirstNum &lt; 326)">
                                <xsl:value-of select="concat('er:#Link_PT_09/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'VIII') or ($nonStringFirstNum > 306 and $nonStringFirstNum &lt; 310)">
                                <xsl:value-of select="concat('er:#Link_PT_08/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'VII') or ($nonStringFirstNum > 305 and $nonStringFirstNum &lt; 307)">
                                <xsl:value-of select="concat('er:#Link_PT_07/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'VI') or ($nonStringFirstNum > 296 and $nonStringFirstNum &lt; 303)">
                                <xsl:value-of select="concat('er:#Link_PT_06/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'V') or ($nonStringFirstNum > 168 and $nonStringFirstNum &lt; 297)">
                                <xsl:value-of select="concat('er:#Link_PT_05/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'IV') or ($nonStringFirstNum > 114 and $nonStringFirstNum &lt; 169)">
                                <xsl:value-of select="concat('er:#Link_PT_04/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'II') or ($nonStringFirstNum > 36 and $nonStringFirstNum &lt; 80)">
                                <xsl:value-of select="concat('er:#Link_PT_02/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:when test="matches(regex-group(1),'LT') or ($nonStringFirstNum >= 0 and $nonStringFirstNum &lt; 3)">
                                <xsl:value-of select="concat('er:#Link_PT_01/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="concat('er:#Link_CH_02/P',regex-group(1),'-',regex-group(2))"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <a href="{$refVar}">
                        <xsl:value-of select="."/>
                    </a>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </td>
    </xsl:template>

当我运行此IVA.004时,我的输出为<a href="er:#Link_PT_05/PIVA-004">IVA.004</a>而不是<a href="er:#Link_PT_04/PIVA-004">IVA.004</a>。请让我知道我哪里出错了,我该如何解决这个问题。这是一个工作小提琴。

http://xsltransform.net/jz1PuNJ

由于

2 个答案:

答案 0 :(得分:1)

字符串IVA.004'V''IV'匹配,在xsl:choose/xsl:when中使用第一个真xsl:when,因此您需要更改顺序xsl:choose

答案 1 :(得分:1)

在xsl:choose中,首先可能是xsl:when条件只返回第一个。即, 'matches(regex-group(1),'V')'将首先返回,因为表达式包含'V', 我的意思是'IVA.004'包含'V',没有机会检查下一个xsl:when,因为在成功条件之后,控制将退出进一步检查。

更改顺序xsl:如下所示。

<xsl:when test="matches(regex-group(1),'IV') or ($nonStringFirstNum > 114 and $nonStringFirstNum &lt; 169)">
    <xsl:value-of select="concat('er:#Link_PT_04/P',regex-group(1),'-',regex-group(2))"/>
</xsl:when><!--Keep this xsl:when first-->
<xsl:when test="matches(regex-group(1),'V') or ($nonStringFirstNum > 168 and $nonStringFirstNum &lt; 297)">
    <xsl:value-of select="concat('er:#Link_PT_05/P',regex-group(1),'-',regex-group(2))"/>
</xsl:when><!--Keep this xsl:when next-->

少数xsl:当订单也需要改变如下

<xsl:when test="matches(regex-group(1),'IX') or ($nonStringFirstNum > 323 and $nonStringFirstNum &lt; 326)">
    <xsl:value-of select="concat('er:#Link_PT_09/P',regex-group(1),'-',regex-group(2))"/>
</xsl:when>
<xsl:when test="matches(regex-group(1),'X') or ($nonStringFirstNum > 325 and $nonStringFirstNum &lt; 332)">
    <xsl:value-of select="concat('er:#Link_PT_10/P',regex-group(1),'-',regex-group(2))"/>
</xsl:when>