4

你好,我是 XSL 的初学者,几乎不知道几个命令。我正在尝试一个示例,我必须根据 XML 中的条目格式化一个数字。我想使用 format-number 函数来实现相同的目的。

<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 :我想将 ###.###,00 传递给 format-number 方法(注意小数点和千位分隔符)或者如果格式是 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 中存在的格式获得输出

谢谢斯里瓦察

4

3 回答 3

4

XSLT 有<xsl:decimal-format>专门针对这种情况的指令

这种转变:

<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>
于 2010-11-19T17:40:29.490 回答
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' 很好。

于 2010-11-19T11:02:04.487 回答
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> 

$format 用于当您有一个由<xslt:variable/>定义的名为 'format' 的变量时。测试条件接受 XPath 语句,如 Format(Details 的节点子节点)/text()(Format 的文本子节点)

于 2010-11-19T10:00:31.183 回答