XSLT:如何使用格式数字和十进制格式? [英] XSLT: How to use format-number and decimal-format?

查看:26
本文介绍了XSLT:如何使用格式数字和十进制格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这样的问题,但我还是不明白.

I know such questions have been asked before but I still don't get it.

我使用 .NET 进行 XSL 转换.

I am using .NET for XSL transformations.

MSDN 描述了格式-编号为:

MSDN describes format-number as:

将数字转换为字符串.转换是对第一个参数 (number) 中指定的数字进行格式化、使用第二个参数 (string) 中指定的格式并应用在第三个可选参数 (string) 中指定的十进制格式中定义的规则的结果.如果省略第三个参数(十进制格式名称),则使用默认十进制格式.此功能和元素旨在一起使用.

Converts numbers into strings. The conversion is the result of formatting the number specified in the first argument (number), using the format specified in the second argument (string), and applying the rules defined in the decimal format named in the third optional argument (string). If the third argument, the decimal format name, is omitted, the default decimal format is used. This function and the element are designed to be used together.

示例

鉴于以下 XML,我想设置一个 XSLT,将小数公开为欧洲符号('.' 作为小数分隔符,',' 作为组分隔符)和英语(或标准符号).

Given the following XML I want to set up a XSLT that exposes the decimals as european notaton ('.' as decimal separator and ',' as group separator) and in english (or standard notation).

data.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Model>
  <Decimal value="1.256"/>
  <Decimal value="19099.33345"/>
</Model>

我使用了一个 XSLT 文档,它声明了两种十进制格式并在选择语句中使用它们,一次是为了以欧洲方式输出,一次以英文表示法输出:

I use a XSLT document that declares both decimal formats and uses them in the select-statement, one time in order to output in european and one time to output in english notation:

trafo.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:decimal-format name="european" decimal-separator=',' grouping-separator='.' />
    <xsl:decimal-format name="en" decimal-separator='.' grouping-separator=',' />

  <xsl:template match="/Model">

      <xsl:for-each select="Decimal">
        Auf european: <xsl:value-of select="format-number(@value, '###.###,00', 'european')"/>
        <!--Auf englisch: <xsl:value-of select="format-number(@value, '###.###,00', 'en')"/>-->
      </xsl:for-each>

      </xsl:template>
</xsl:stylesheet>

我的期望是转换将输出如下内容:

My expectation is that the transformation will output something like:

european: 1,256
english: 1.256
european: 19.099,33345
english: 19,099.33345

但相反,我在此处也提到了异常:

But instead I get the exception also mentioned here:

格式 '#,###.00' 在小数点后的数字符号后不能有零数字符号."

"Format '#,###.00' cannot have zero digit symbol after digit symbol after decimal point."

所以当我需要更改英文和欧洲符号的格式模式时,十进制格式的目的是什么?

So when I need to change the format pattern for english and european notations what is the purpose of decimal-format at all?

为了完整起见,这里是我用于转换的 C# 代码

For completeness, here is the C# code I used for the transformation

        XDocument data = XDocument.Load("data.xml");
        XDocument xslt = XDocument.Load("trafo.xslt");

        XslCompiledTransform compiled = new XslCompiledTransform();
        using (var reader = xslt.CreateReader())
        {
            compiled.Load(reader);
        }

        // wir wollen auch Fragmente zulassen
        var writerSettings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Auto };
        var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Auto };
        var stringReader = new StringReader(data.ToString());
        var details = new StringBuilder();
        using (var reader = XmlReader.Create(stringReader, readerSettings))
        {
            using (var writer = XmlWriter.Create(details, writerSettings))
            {
                compiled.Transform(reader, writer);
            }
        }

        Console.WriteLine(details.ToString());

推荐答案

十进制模式(format-number 的第三个参数)说明哪些字符用作两个分隔符.

Decimal pattern (the 3rd argument of format-number) says which chars serve as both separators.

第一条指令 format-number(@value, '###.###,00', 'european')"/看起来不错,因为 european 十进制格式将十进制分隔符设置为一个逗号.

The first instruction format-number(@value, '###.###,00', 'european')"/ looks OK, since european decimal-format has decimal-separator set to just a comma.

但是第二条指令 format-number(@value, '###.###,00', 'en') 导致一个错误,因为:

But the second instruction format-number(@value, '###.###,00', 'en') causes an error, because:

  • en 十进制格式的十进制分隔符设置为
  • 您在此处指定了一个逗号.
  • en decimal-format has decimal-separator set to a dot,
  • you specified in this place a comma.

将第二条指令改为format-number(@value, '###,###.00', 'en')(注意两个分隔符都改变了).

Change the second instruction to format-number(@value, '###,###.00', 'en') (note both separators changed).

如果错误消息以这种方式表述可能会更容易理解:

Maybe it would be more understandable if the error message had been formulated this way:

  • 在您传递"将整数部分与小数部分分开的字符后(在欧洲 - 逗号,在英语 - 一个点),
  • 不允许有#字符(ddigit占位符,可以更改到一个空格),
  • 然后是一个0"字符(零位占位符).
  • After you "pass" the char separating integer part from fractional one (in european - a comma, in English - a dot),
  • it is not allowed to have a # char (ddigit placeholder, can be changed to a space),
  • and after it a '0' char (zero digit placeholder).

在您的(英文)示例中,您实际上有:

In your (English) example you have actually:

  • 整数部分的 3 位(数字符号),
  • 一个小数点(一个点 - 英文版),
  • 小数部分的另外3位(也是数字符号),到目前为止,所有数字都可以更改为空格,
  • 一个分组分隔符(逗号),
  • 另外 2 位数字( 数字符号),这就是原因错误消息.
  • 3 places (digit symbols) for integer part,
  • a decimal point (a dot - English version),
  • another 3 places (also digit symbols) for fractional part, till now all digits can be changed to spaces,
  • a grouping separator (a comma),
  • another 2 digit places (zero digit symbols), and this is the cause of the error message.

这篇关于XSLT:如何使用格式数字和十进制格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆