【问题标题】:Padding number with leading zeros in XSLT 1.0在 XSLT 1.0 中使用前导零填充数字
【发布时间】:2014-10-29 00:09:38
【问题描述】:

我们有一个 XML 中的数字,在一个大型 XML 文件中最多可以达到 3 位数字,必须将其转换为固定长度的文本才能加载到另一个系统中。

我需要在输出中用前导零填充长度为 15(这是固定长度的文本)

例子:

 - 1 becomes   000000000000001
 - 11 becomes  000000000000011
 - 250 becomes 000000000000250

我试过了:

<xsl:value-of select="substring(concat('000000000000000', msg:BankAccount/msg:Counter), 12, 15)"/>

在开头得到 15 个零并取子字符串,但我一定是在子字符串上犯了一个错误,因为在结果中我得到了

0000000000000000000000009LLOYDS BANK PLC
00000000000000000000000010LLOYDS BANK PLC

我也尝试了format-number,但它返回 NaN

<xsl:value-of select="format-number(msg:BankAccount/msg:Counter, '000000000000000')"/>

返回“NaN”

那么我做错了什么,最好的方法是什么?

【问题讨论】:

  • msg:Counter 末尾有“LLOYDS BANK PLC”吗? format-number() 仅适用于数字。
  • 不,它只有一个数字,我保存在'LLOYDS BANK PLC'中以显示错误导致的效果
  • 如果 msg:Counter 是一个数字,那么 format-number 没有理由返回 NaN。您没有告诉我们一些问题。

标签: xml xslt-1.0 string-formatting msxml6


【解决方案1】:

我需要在输出中用前导零填充长度为 15(

那就是

substring(
  concat('000000000000000', msg:BankAccount/msg:Counter), 
  string-length(msg:BankAccount/msg:Counter) + 1, 
  15
)

【讨论】:

  • 感谢您的帮助,非常感谢,我没有考虑过这样检查长度。
【解决方案2】:

另一种方法是

substring(string(1000000000000000 + $x), 2)

【讨论】:

  • 是的,我假设 $x 是一个数字,或者是一个将被转换为数字的值(细节取决于它是 XPath 1.0 还是 2.0)。
  • 非常优雅的解决方案
  • 完美。接受的答案对我不起作用,但这确实而且更简洁!
【解决方案3】:

也可以使用字符串格式

<xsl:value-of select="format-number(msg:BankAccount/msg:Counter, '000000000000000')" />

一般:

<xsl:value-of select="format-number(number_you_would_like_to_padd, 'string_how_much_zeros_you_would like')" />

【讨论】:

    【解决方案4】:

    另一个选项是xsl:number...

    <xsl:number value="number_to_format" format="000000000000001"/>
    

    完整示例...

    XML 输入

    <doc>
        <test>1</test>
        <test>11</test>
        <test>250</test>
    </doc>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="test">
        <xsl:number value="." format="000000000000001"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:template>
    
    </xsl:stylesheet>
    

    输出

    000000000000001
    000000000000011
    000000000000250
    

    小提琴:http://xsltfiddle.liberty-development.net/pPzifpg

    【讨论】:

      【解决方案5】:

      要考虑的另一种选择....

      <xsl:apply-templates select="Groups/Group[@Name='TheSource']/Field[@Name='BankAccount']" />
      <xsl:text>999999999999999</xsl:text>
      <!-- uncomment below and comment above line to use without test number -->
      <!-- <xsl:text>|</xsl:text> -->
      
      <xsl:template match="Groups/Group[@Name='BankAcctFile']/Field[@Name='BankAccount']">
          <xsl:call-template name="padleft">
              <xsl:with-param name="padChar" select="'0'" />
              <xsl:with-param name="padVar" select="substring(current(),1,15)" />
              <xsl:with-param name="length" select="15" />
          </xsl:call-template>
      </xsl:template>
      

      【讨论】:

      • 没有被调用模板的内容,这并不能提供问题的答案。
      • 上面添加的调用模板示例的内容。
      • 不,不是。
      猜你喜欢
      • 2021-02-19
      • 1970-01-01
      • 2013-03-22
      • 2012-04-21
      • 1970-01-01
      • 2022-01-18
      • 2015-05-21
      • 1970-01-01
      • 2014-09-19
      相关资源
      最近更新 更多