2013-03-25 58 views
1

嗨,我有以下的XML。xsl如果在xsl如果

<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> 
+0

請包括一個完整的例子,或添加splitByHyphen和splitByComma模板。 – Treemonkey 2013-03-25 11:18:25

+0

嗨,我已經用連字符和逗號模板更新了xslt – 2013-03-25 12:22:19

+0

您的預期結果的發佈代碼似乎是錯誤的,您正在關閉沒有文字的''標籤 – Vloxxity 2013-03-25 12:53:19

回答

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> 

僅使用電流(改變這個下面部分),而不是當前的()/文本( )

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