2010-11-19 76 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