【问题标题】:Using XSLT to pad zeros to numbers in a string使用 XSLT 将零填充到字符串中的数字
【发布时间】:2017-01-11 16:51:16
【问题描述】:

我有一个 XSLT(1.0 版)来挑选一个包含数字的字符串,但并非所有数字都有前导零。问题是这个字符串现在被用来按字母顺序对元素进行排序。

理想情况下,运行 XSLT(以及其他更改)应该改变:

<atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
<atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>

收件人:

<field name="heirarchy_sequence">BODA.04.03.60</field>
<field name="heirarchy_sequence">BODA.04.03.60</field>

如何重写我们的 XSLT,以便在必要时用零填充该字符串中的数字? 另一个小问题是字符串中文本和数字之间的分隔符并不总是“。”有时它可能是一个“-”。

当我声明 PHP 命名空间时,我想我可能会使用 PHP 函数来分解字符串并使用 sprintntf 来格式化数字部分,但我觉得这行不通……

我的 XSLT 如下所示:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xlink="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="add">
    <add>
        <xsl:apply-templates />
    </add>
</xsl:template>
<xsl:template match="doc">
    <xsl:copy>
        ...     
            <field name="heirarchy_sequence">
                 <xsl:value-of select="atom[@name='EADUnitID'][normalize-space()]"/>
            </field>
        ...
        </xsl:copy>
</xsl:template>

它正在更改的 XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
<doc name="record">
    <atom name="irn" type="text" size="short">6135</atom>
    <atom name="ObjectType" type="text" size="short">Archives</atom>
    <atom name="EADLevelAttribute" type="text" size="short">Item</atom>
    <atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
  </doc>
  <doc name="record">
    <atom name="irn" type="text" size="short">6136</atom>
    <atom name="ObjectType" type="text" size="short">Archives</atom>
    <atom name="EADLevelAttribute" type="text" size="short">Item</atom>
    <atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>
  </doc>
  </xml_split:root>

编辑 1

从 xml 示例中删除了不必要的 &lt;/table&gt;

编辑 2 - 变体示例

<atom name="EADUnitID" type="text" size="short">gls-1-1-1</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-2</atom>

【问题讨论】:

  • 你不能被. 分解并检查字符串的长度。如果它低于预期添加多少位数?
  • 由于&lt;/table&gt; 结束标记不匹配,您的 XML 无效。这应该是您数据的真实部分吗?
  • 对您必须处理的输入进行适当的规范会很有用,而不仅仅是两个示例输入和一个可能会发生变化的声明。
  • @JohnBollinger 这是一个错字 - 记录包含更多行和元组,其中没有一个是相关的,所以我试图删除它们。不过一定错过了那个结束标签——好地方。我已编辑问题以删除标签。
  • @MichaelKay - 抱歉,我将添加其他变体的示例。

标签: php xml xslt


【解决方案1】:

假设您使用的是libxslt 处理器,您可以这样做:

<xsl:template match="atom[@name='EADUnitID']">
    <field name="heirarchy_sequence">
        <xsl:variable name="tokens" select="str:tokenize(., '.-')" />
            <xsl:value-of select="$tokens[1]" />
        <xsl:for-each select="$tokens[position() > 1]">
            <xsl:text>.</xsl:text>
            <xsl:value-of select="str:align(., '0000', 'right')"/>
        </xsl:for-each>
    </field>
</xsl:template>

转换:

<atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
<atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-1</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-2</atom>

进入:

<field name="heirarchy_sequence">BODA.0004.0003.0060</field>
<field name="heirarchy_sequence">BODA.0004.0003.0061</field>
<field name="heirarchy_sequence">gls.0001.0001.0001</field>
<field name="heirarchy_sequence">gls.0001.0001.0002</field>

见:http://exslt.org/str/index.html

【讨论】:

  • P.S.这不是你拼写“层次结构”的方式。
猜你喜欢
  • 2010-09-24
  • 2012-02-17
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
相关资源
最近更新 更多