2010-11-19 71 views
4

你好 對於XSL來說,初學者幾乎不知道幾個命令。 我正在嘗試一個示例,我必須根據XML中的條目格式化數字。 我想使用格式編號功能來實現相同。XSL需要幫助

<Details> 
<Order>Bulk Order</Order> 
<OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
<Quantity>100</Quantity> 
<Price>99.45</Price> 
<Format>de_DE</Format> 
</Details> 


<Details> 
<Order>Bulk Order</Order> 
<OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
<Quantity>100</Quantity> 
<Price>99.45</Price> 
<Format>en_US</Format> 
</Details> 

但是我可以渲染輸出,如果我使用:

<xsl:value-of select='format-number(500100, "###,###.00")' /> 

但我想用一定的條件

即如果格式是de_DE這個: 我想通過#格式編號方法(注意小數點和千位分隔符) 或格式爲en_US 我想將###,###。00傳遞給格式編號方法

我無望的使用選擇語句(但我真的沒有對使用的語法想法)試圖

<xslt:choose> 
    <xslt:when test="$format = 'de_DE'">###,###.00</xslt:when> 
    <xslt:when test="$format = 'en_US'">###.###,00</xslt:when> 
    <xslt:otherwise>###.###,00</xslt:otherwise> 
</xslt:choose> 

誰能幫我把這個拖到模板或東西,這樣我只是叫 我也得到基於存在於XML

感謝 Srivatsa

+0

好問題,+1。查看我的答案以獲得最佳,真正的XSLT解決方案。請隨時接受我的回答:) – 2010-11-19 17:41:36

+0

@@ this-Me:現在不是接受*最佳答案的時候嗎? – 2011-09-17 20:51:13

回答

1

您可以應用模板上的文本節點的值和匹配如下:

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


<xsl:template match="/">   
    <xsl:apply-templates select="/root/Details"/> 
</xsl:template> 

<xsl:template match="Details">  

    <xsl:variable name="total" select="Price * Quantity"/> 

    <xsl:apply-templates select="Format"> 
     <xsl:with-param name="total" select="$total"/> 
    </xsl:apply-templates> 

</xsl:template> 

<xsl:template match="Format[text()='de_DE']"> 
    <xsl:param name="total"/>  
    <xsl:value-of select="format-number($total, '###.###.00')"/> 
</xsl:template> 

<xsl:template match="Format[text()='en_US']"> 
    <xsl:param name="total"/>  
    <xsl:value-of select="format-number($total, '###,###.00')"/> 
</xsl:template> 

此代碼例如,匹配所有細節節點和每場比賽得到的總的訂貨。然後,它會對傳入總數的格式應用模板作爲參數。匹配然後發生在格式節點的值上。

我認爲格式'###。###。00'是無效的,因爲它似乎只允許一個小數點。 '###,###。00'很好。

+0

你好埃利斯,我從來沒有這種格式###。###。00無論如何:D – 2010-11-19 12:33:08

+0

但是,謝謝你的解決方案 – 2010-11-19 12:33:25

+0

哦,是的,你是對的,我的眼睛欺騙了我! – Ellis 2010-11-19 12:37:43

0

格式輸出假設你在一個模板匹配「詳細資料」的節點,那麼你可以做一些很像這樣的事情:

<xslt:choose> 
    <xslt:when test="Format/text() = 'de_DE'"><xsl:value-of select="format-number(Price, '###,###.00')" /></xslt:when> 
    <xslt:when test="Format/text() = 'en_US'"><xsl:value-of select="format-number(Price, '###.###,00')" /></xslt:when> 
    <xslt:otherwise><xsl:value-of select='format-number(Price, "###.###,00")' /></xslt:otherwise> 
</xslt:choose> 

$格式是當你有一個命名爲< XSLT定義的 '格式' 變量:變量/ >。測試條件接受XPath語句就像格式(詳細的子節點)/文()(格式的文本子節點)

+0

這是''的用途。看到我的答案。 – 2010-11-19 18:40:37

4

XSLT有​​指令,特別是對於這種情況

這種變換:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:decimal-format name="de_DE" decimal-separator="." grouping-separator="," /> 
<xsl:decimal-format name="en_US" decimal-separator="," grouping-separator="."/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Price/text()"> 
    <xsl:value-of select="format-number(., '#,###.##', ../../Format)"/> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加(裹成頂部元件節點進行良好的形成):

<t> 
    <Details> 
     <Order>Bulk Order</Order> 
     <OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
     <Quantity>100</Quantity> 
     <Price>1199.45</Price> 
     <Format>de_DE</Format> 
    </Details> 
    <Details> 
     <Order>Bulk Order</Order> 
     <OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
     <Quantity>100</Quantity> 
     <Price>1199.45</Price> 
     <Format>en_US</Format> 
    </Details> 
</t> 

產生想要的結果

<t> 
    <Details> 
     <Order>Bulk Order</Order> 
     <OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
     <Quantity>100</Quantity> 
     <Price>1,199.45</Price> 
     <Format>de_DE</Format> 
    </Details> 
    <Details> 
     <Order>Bulk Order</Order> 
     <OrderDate>1997-07-16T19:20:30+01:00</OrderDate> 
     <Quantity>100</Quantity> 
     <Price>1199,45</Price> 
     <Format>en_US</Format> 
    </Details> 
</t> 
+0

+1爲正確的答案。 – 2010-11-19 18:49:48

+0

不錯。非常好。 +1 – 2010-11-19 18:51:45