【问题标题】:XSL Help required需要 XSL 帮助
【发布时间】:2010-11-19 09:46:49
【问题描述】:

你好 我是 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 传递给 format-number 方法

我绝望地尝试使用选择语句(但我真的不知道用法的语法)

<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 中存在的格式获得输出

谢谢 斯里瓦萨

【问题讨论】:

  • 好问题,+1。请参阅我的答案以获得最佳、真正的 XSLT 解决方案。随意接受我的回答:)
  • @@this-Me:现在是时候接受最佳答案了吗?

标签: xml xslt xslt-1.0


【解决方案1】:

XSLT 有 &lt;xsl:decimal-format&gt; 指令,特别适用于这种情况

这种转变:

<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>

【讨论】:

    【解决方案2】:

    您可以应用模板并匹配文本节点的值,如下所示:

    <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' 很好。

    【讨论】:

      【解决方案3】:

      假设您在“详细信息”节点的模板匹配中,那么您可以执行以下操作:

      <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 用于当您有一个由 定义的名为“format”的变量时。测试条件接受 XPath 语句,如 Format(Details 的节点子节点)/text()(Format 的文本子节点)

      【讨论】:

        猜你喜欢
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 2022-01-04
        • 2017-10-05
        • 2017-03-23
        相关资源
        最近更新 更多