2009-02-23 49 views
1

XSL有一個有趣的問題。我們有一個XML輸入文件,其中包含內容旁邊的幾個設置,用於定義我們如何輸出內容。目前我正在努力處理XSL文件中的數字格式。XSL:具有多種可能格式的十進制格式

以下格式是可能的:

  • 1,234.5
  • 1.234,5
  • 1234,5
  • 1234.5

我得到這些格式作爲我上面的XML文件中提到的:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <settings> 
     <DefaultCurrency>EUR</DefaultCurrency> 
     <DefaultDateFormat>DD.MM.YYYY</DefaultDateFormat> 
    <DefaultNumberFormat>1.250,65</DefaultNumberFormat> 
    <AllowOrdering>true</AllowOrdering> 
</settings> 
<productlist> 
    <item> 
     <product> 
      <productid>Product1</productid> 
      <weight>0.123</weight> 
     </product> 
     <customerprice>123.03</customerprice> 
    </item> 
    <item> 
     <product> 
      <productid>Product2</productid> 
      <weight>12312.123</weight> 
     </product> 
     <customerprice>12.00</customerprice> 
    </item> 
    <item> 
     <product> 
      <productid>Product3</productid> 
      <weight>12.123</weight> 
     </product> 
     <customerprice>13.23</customerprice> 
    </item> 
</productlist> 
</document> 

我試圖得到這個工作與以下的XSL文件,但我不能讓它給我正確的結果(當XML中的DefaultNumberFormat改變格式也必須改變)。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
    <xsl:decimal-format name="numberformat1" decimal-separator="." grouping-separator=","/> 
    <xsl:decimal-format name="numberformat2" decimal-separator=","/> 
    <xsl:decimal-format name="numberformat3" decimal-separator="," grouping-separator="."/> 
    <xsl:decimal-format name="numberformat4" decimal-separator="."/> 
    <xsl:template match="productlist"> 
     <xsl:apply-templates select="item"/> 
    </xsl:template> 
    <xsl:template match="item"> 
ItemID: <xsl:value-of select="product/productid"/> 
Weigh: 
<xsl:choose> 
      <xsl:when test="/document/settings/DefaultNumberFormat = '1,250.65'"> 
       <xsl:value-of select="format-number(product/weight, '#,##0.000', 'numberformat1')"/> 
      </xsl:when> 
      <xsl:when test="/document/settings/DefaultNumberFormat = '1250,65'"> 
       <xsl:value-of select="format-number(product/weight, '#,##0.000', 'numberformat2')"/> 
      </xsl:when> 
      <xsl:when test="/document/settings/DefaultNumberFormat = '1.250,65'"> 
       <xsl:value-of select="format-number(product/weight, '#,##0.000', 'numberformat3')"/> 
      </xsl:when> 
      <xsl:when test="/document/settings/DefaultNumberFormat = '1250.65'"> 
       <xsl:value-of select="format-number(product/weight, '#,##0.000', 'numberformat4')"/> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:template> 
    <xsl:template match="settings/*"/> 
</xsl:stylesheet> 

我希望有人能幫助我在這裏,我想我不知怎麼的怎麼號格式和數字格式結合了心靈的扭曲......

感謝,馬亭

回答

0

下列轉換計算數格式使用哪個

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:variable name="vDoc" select="/"/> 

    <xsl:variable name="vDFormats"> 
     <dFormat spec="1,250.65" name="numberformat1"/> 
     <dFormat spec="1250,65" name="numberformat2"/> 
     <dFormat spec="1.250,65" name="numberformat3"/> 
     <dFormat spec="1250.65" name="numberformat4"/> 
    </xsl:variable> 

    <xsl:variable name="vDefFormats" select= 
    "document('')/*/xsl:variable 
         [@name='vDFormats']"/> 

    <xsl:variable name="vtheFormatName" select= 
    "string($vDefFormats/* 
       [@spec = $vDoc/*/settings/DefaultNumberFormat] 
            /@name 
      ) 
    "/> 

    <xsl:decimal-format name="numberformat1" 
     decimal-separator="." grouping-separator=","/> 
    <xsl:decimal-format name="numberformat2" 
     decimal-separator=","/> 
    <xsl:decimal-format name="numberformat3" 
     decimal-separator="," grouping-separator="."/> 
    <xsl:decimal-format name="numberformat4" 
     decimal-separator="."/> 

    <xsl:template match="productlist"> 
     <xsl:apply-templates select="item"/> 
    </xsl:template> 

    <xsl:template match="item">ItemID: <xsl:text/> 
     <xsl:value-of select="product/productid"/> Weigh: <xsl:text/> 
     <xsl:value-of select= 
     "format-number(product/weight, 
         '#,##0.000', 
         $vtheFormatName)"/> 
    </xsl:template> 

    <xsl:template match="settings/*"/> 
</xsl:stylesheet> 

當應用於ö n中的下面的XML文檔

<document> 
    <settings> 
    <DefaultNumberFormat>1,250.65</DefaultNumberFormat> 
    </settings> 

    <productlist> 
     <item> 
     <product> 
      <productid>30</productid> 
      <weight>2530.45</weight> 
     </product> 
     </item> 
    </productlist> 
</document> 

有用結果產生

ItemID: 30 Weigh: 2,530.450 
+0

「0」格式符號可能不遵循該「#」的格式碼元的格式圖案的該部分 – Martijn 2009-02-23 17:18:18

3

在XSLT,format-number是一個普通的XPath函數,它將字符串作爲第二個和第三個參數。這些字符串不需要在樣式表中指定爲常量 - 它們也可以在運行時從源文檔中計算出來。

在最簡單的情況下,讓我們假設源文檔實際上將參數直接指定爲settings/picture`settings/formatname'。如果是這樣,你可以寫:

<xsl:value-of select="format-number(product/weight, 
            settings/picture, 
            settings/formatname)"/> 

另外,如果文檔使用了不同的符號,使得轉換步驟有必要,你可以重構你的樣式表使用XSLT變量是這樣的:

<xsl:variable name="picture"> 
    <!-- compute the picture string here, e.g. using <xsl:choose> and --> 
    <!-- /document/settings/DefaultNumberFormat --> 
</xsl:variable> 

<xsl:variable name="formatname"> 
    <!-- compute the number format name here, e.g. using <xsl:choose> and --> 
    <!-- /document/settings/DefaultNumberFormat --> 
</xsl:variable> 

<xsl:value-of select="format-number(product/weight, $picture, $formatname)"/>