2014-05-20 11 views
0

我有下面的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 

,我想轉換th e x.y納入以下格式

er:#BGCL_CH_x/Px-y 

請讓我知道我該怎麼做。

感謝

回答

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>