xsl if在xsl if中

时间:2013-03-25 10:04:39

标签: xslt

嗨,我有以下xml。

<primaryie>
<content-style font-style="bold">Administration</content-style>
</primaryie>

当我使用下面的xslt时,它工作正常。(内容风格的部分)

    <xsl:template match="primaryie">
    <div class="primaryie">
    <xsl:apply-templates select="content-style"/>
    <xsl:if test="contains(current()/text(), '.')">
    <xsl:variable name="numberString" select="substring(current()/text(), string-length(substring-before(current()/text(),'.'))-1)"></xsl:variable>
    <xsl:call-template name="numbersToLink">
    <xsl:with-param name="numbersString" select="$numberString"></xsl:with-param>
    </xsl:call-template>
    </xsl:if>
    </div>
    </xsl:template>
    <xsl:template name="numbersToLink">
        <xsl:param name="numbersString"></xsl:param>
        <xsl:choose>
            <xsl:when test="contains($numbersString, ',')">
                <xsl:call-template name="splitByComma">
                    <xsl:with-param name="numString" select="$numbersString"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($numbersString, '-')">
                <xsl:call-template name="splitByHyphen">
                    <xsl:with-param name="numString" select="$numbersString"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="5 >= string-length(normalize-space($numbersString))">
                <xsl:variable name="x">
                    <xsl:value-of select="substring-after($numbersString,'.')"></xsl:value-of>
                </xsl:variable>
                <xsl:variable name="y">
                    <xsl:value-of select="normalize-space(substring-before($numbersString,'.'))"></xsl:value-of>
                </xsl:variable> 

                <xsl:variable name="conca">
                    <xsl:value-of select="concat('er:#BVI_CH_0',$y,'/P',$y,'-',$x)"/>
                </xsl:variable>    
                <a href="{$conca}">
                    <xsl:value-of select="$numbersString"/>
                </a>
            </xsl:when>
            <xsl:otherwise>

            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
 <xsl:template name="splitByComma">
        <xsl:param name="numString"></xsl:param>
        <xsl:choose>
            <xsl:when test="contains(substring-before($numString,','), '-')">
                <xsl:call-template name="splitByHyphen">
                    <xsl:with-param name="numString" select="$numString"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($numString,',')">
<xsl:variable name="abc">
<xsl:value-of select="normalize-space(substring-before($numString,'.'))"/>
</xsl:variable>
<xsl:variable name="def">
<xsl:value-of select="substring-before(substring-after($numString,'.'),',') "/>
</xsl:variable>
<xsl:variable name="conct">
<xsl:value-of select="concat('er:#BVI_CH_0',$abc,'/P',$abc,'-',$def)"/>
<!--"concat(concat('er:#BVI_CH_0',,'/P',$y,'-',$x)"/-->
</xsl:variable>
                <a href="{$conct}">
                    <xsl:value-of select="substring-before($numString,',')"/>
                </a>
                <xsl:text>, </xsl:text>
                <xsl:if test="contains(substring-after($numString,','), '.')">
                    <xsl:call-template name="numbersToLink">
                        <xsl:with-param name="numbersString" select="normalize-space(substring-after($numString,','))"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>


    <xsl:template name="splitByHyphen">
        <xsl:param name="numString"></xsl:param>
        <xsl:choose>
            <xsl:when test="contains($numString,'-')">
<xsl:variable name="abc">
<xsl:value-of select="normalize-space(substring-before($numString,'.'))"/>
</xsl:variable>
<xsl:variable name="def">
<xsl:value-of select="substring-before(substring-after($numString,'.'),'-') "/>
</xsl:variable>
<xsl:variable name="conct">
<xsl:value-of select="concat('er:#BVI_CH_0',$abc,'/P',$abc,'-',$def)"/>
<!--"concat(concat('er:#BVI_CH_0',,'/P',$y,'-',$x)"/-->
</xsl:variable>
                <a href="{$conct}">
                    <!--<xsl:value-of select="substring-before($numString,'-')"/>-->
                    <xsl:value-of select="substring-before($numString,'-')"/>
                </a>
                <xsl:text>&#x2013;</xsl:text>
                <xsl:if test="contains(substring-after($numString,'-'), '.')">
                    <xsl:call-template name="numbersToLink">
                        <xsl:with-param name="numbersString" select="normalize-space(substring-after($numString,'-'))"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

但是我在同一文档中有一个特殊情况,如下面的xml状态。

<primaryie>
 <content-style font-style="bold">VIRRGIN system</content-style> 7.204, 7.205
</primaryie>

这里当我使用上面的模板时,它正在使用内容样式,但我希望它也可以使用numberstolink模板。

输出如下。

电流:

 <div class="primaryie"><span class="font-style-bold">Virgin Islands Special Trust Act (VISTA) 9.077</span></div>

expexted:

 <div class="primaryie"><span class="font-style-bold">Virgin Islands Special Trust Act (VISTA)<a href="er:#BVI_CH_09/P9-077"> 9.077</a></span></div>

1 个答案:

答案 0 :(得分:1)

请在代码中更改以下部分

<xsl:template match="primaryie">
    <div class="primaryie">
        <xsl:apply-templates select="content-style"/>
        <xsl:if test="contains(current(), '.')">
            <xsl:variable name="numberString" select="substring(current(), string-length(substring-before(current(),'.'))-1)"></xsl:variable>
            <xsl:call-template name="numbersToLink">
                <xsl:with-param name="numbersString" select="$numberString"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </div>
</xsl:template>

仅使用Current()而不是Current()/ Text()

给出XML的输出是

<div class="primaryie">VIRRGIN system<a href="er:#BVI_CH_07/P7-204"> 7.204</a>, <a href="er:#BVI_CH_07/P7-205">7.205</a></div>