1

嗨,我有以下 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>

解释:

 <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>
4

1 回答 1

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>
于 2014-06-17T18:07:25.830 回答