xslt 中十进制格式的条件选择

发布于 2024-08-25 07:57:47 字数 807 浏览 3 评论 0原文

我正在尝试根据 XML 的某些信息修改样式表的十进制格式。更准确的是,我有一个像这样的 XML,

<?xml version="1.0" encoding="ISO-8859-1"?>
<REPORT>
   <LANGUAGE>2</LANGUAGE>
   <MYVALUE>123456.78</MYVALUE>
</REPORT>

如果语言为 2,我试图将十进制格式定义为欧洲格式,否则为默认格式。所以我创建了以下模板,

<xsl:template match="REPORT">
     <xsl:if test="$language=2">
          <xsl:decimal-format decimal-separator=',' grouping-separator='.' />
     </xsl:if>
     <xsl:value-of select ="format-number(MYVALUE,'###.###,00')"/>
</xsl:template>

它以欧洲格式或标准格式显示数字。但我收到以下错误

xsl:decimal-format is not allowed in thisposition in the stylesheet!

如果我尝试将十进制格式放在模板之外,则会收到一条消息,指出 sthylsheet 中的此位置不允许使用 xsl:if。如何更改 XML 中的十进制格式?

谢谢 何塞

I'm trying to modify the decimal-format of a stylesheet based on certain information of an XML. More exaclty, I've a XML like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<REPORT>
   <LANGUAGE>2</LANGUAGE>
   <MYVALUE>123456.78</MYVALUE>
</REPORT>

I'm trying to define the decimal format as european if the language is 2, and default otherwse. So I've created the following template

<xsl:template match="REPORT">
     <xsl:if test="$language=2">
          <xsl:decimal-format decimal-separator=',' grouping-separator='.' />
     </xsl:if>
     <xsl:value-of select ="format-number(MYVALUE,'###.###,00')"/>
</xsl:template>

So it shows the number in european format or in standard format. But I'm getting the following error

xsl:decimal-format is not allowed in this position in the stylesheet!

If I try to put the decimal-format outside the template, then I get the message that the xsl:if is not allowed in this position in the sthylsheet. How can I change the decimal-format based in the XML?

Thanks
Jose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-09-01 07:57:47

该元素只能在该元素的正下方定义。定义后,您可以在 fromat-number 函数中使用定义的格式。

<xsl:stylesheet ... namespaces, etc. ...>
    <xsl:decimal-format name="de" decimal-separator=',' grouping-separator='.'/>
    <xsl:decimal-format name="us" decimal-separator='.' grouping-separator=','/>

    <xsl:param name="numFormat">
       <xsl:choose>
           <xsl:when test="/REPORT/@language = 2">
               <xsl:text>us</xsl:text>
           </xsl:when>
           <xsl:otherwise>
               <xsl:text>de</xsl:text>
           </xsl:otherwise>
       </xsl:choose>
    </xsl:param>

    <xsl:template match="REPORT">
        <xsl:choose>
            <xsl:value-of select="format-numer(MYVALUE, '###.###,00', $numFormat)"/>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

The element can only defined directly underneat the element. After definition you then can use the defined formats in the fromat-number function.

<xsl:stylesheet ... namespaces, etc. ...>
    <xsl:decimal-format name="de" decimal-separator=',' grouping-separator='.'/>
    <xsl:decimal-format name="us" decimal-separator='.' grouping-separator=','/>

    <xsl:param name="numFormat">
       <xsl:choose>
           <xsl:when test="/REPORT/@language = 2">
               <xsl:text>us</xsl:text>
           </xsl:when>
           <xsl:otherwise>
               <xsl:text>de</xsl:text>
           </xsl:otherwise>
       </xsl:choose>
    </xsl:param>

    <xsl:template match="REPORT">
        <xsl:choose>
            <xsl:value-of select="format-numer(MYVALUE, '###.###,00', $numFormat)"/>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
纵性 2024-09-01 07:57:47

decimal-format 必须是顶级元素,但是您可以命名它,然后在条件构造中引用该名称,也许像下面这样的内容适合您。

<xsl:decimal-format name="format1" decimal-separator=',' grouping-separator='.' />
<xsl:template match="REPORT">
  <xsl:choose>
    <xsl:when test="$language=2">
      <xsl:value-of select="format-number(MYVALUE,'###.###,00','format1')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="MYVALUE"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

decimal-format must be a top-level element, however you can name it and then refer to the name in a conditional construct, perhaps something like the following will work for you.

<xsl:decimal-format name="format1" decimal-separator=',' grouping-separator='.' />
<xsl:template match="REPORT">
  <xsl:choose>
    <xsl:when test="$language=2">
      <xsl:value-of select="format-number(MYVALUE,'###.###,00','format1')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="MYVALUE"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文