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