在特殊标签之间的数字中添加前导零
Add leading zero to numbers between special tags
我知道在数字中添加前导零并不复杂。但是,我正在寻找一种最佳解决方案,仅将前导零添加到 <SpecialTag>0</SpecialTag>
之间的值,使它们成为 5 位数。
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<Root>
<Row>
<Tag1>0</Tag1>
<SpecialTag>0</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12345</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>1234</SpecialTag>
<Tag2>0</Tag2>
</Row>
</Root>
预期结果应如下所示:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<Root>
<Row>
<Tag1>0</Tag1>
<SpecialTag>00000</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>00012</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12345</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>01234</SpecialTag>
<Tag2>0</Tag2>
</Row>
</Root>
使用xsltproc(建议的解决方案!):
有 XLST 文件 transform.xsl
:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Matches the SpecialTag -->
<xsl:template match="SpecialTag">
<xsl:copy>
<!-- The number is available using node() and format-number() applies the 0-padding -->
<xsl:value-of select="format-number(node(), '00000')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
运行以下,前提是input.xml
包含你的XML:
$ xsltproc transform.xml input.xml
不安全的解决方案:
这些依赖于开始标签 <SpecialTag>
和结束标签 </SpecialTag>
在同一行并且每行只有一个。
下面的解决方案仅被提及,因为作者明确地用sed/bash/shell标记了问题。这些不是完成工作的正确工具!
它们都使用正则表达式来捕获 <SpecialTag>
,然后是几个数字,然后是 </SpecialTag>
并用这些数字的 0 填充版本转换捕获的数字。
使用 sed:
sed --regexp-extended 's@<SpecialTag>([0-9]+)</SpecialTag>@<SpecialTag>0000000</SpecialTag>@;s@0*([0-9]{5,})@@'
使用 perl:
perl -pe 's@<SpecialTag>([0-9]+)</SpecialTag>@sprintf("<SpecialTag>%05d</SpecialTag>",)@e'
使用 awk:
awk '{gsub( /<SpecialTag>[0-9]+<\/SpecialTag>/, sprintf("<SpecialTag>%05d</SpecialTag>", gensub(/[^0-9]/, "","g"))); print}'
我知道在数字中添加前导零并不复杂。但是,我正在寻找一种最佳解决方案,仅将前导零添加到 <SpecialTag>0</SpecialTag>
之间的值,使它们成为 5 位数。
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<Root>
<Row>
<Tag1>0</Tag1>
<SpecialTag>0</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12345</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>1234</SpecialTag>
<Tag2>0</Tag2>
</Row>
</Root>
预期结果应如下所示:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<Root>
<Row>
<Tag1>0</Tag1>
<SpecialTag>00000</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>00012</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>12345</SpecialTag>
<Tag2>0</Tag2>
</Row>
<Row>
<Tag1>0</Tag1>
<SpecialTag>01234</SpecialTag>
<Tag2>0</Tag2>
</Row>
</Root>
使用xsltproc(建议的解决方案!):
有 XLST 文件 transform.xsl
:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Matches the SpecialTag -->
<xsl:template match="SpecialTag">
<xsl:copy>
<!-- The number is available using node() and format-number() applies the 0-padding -->
<xsl:value-of select="format-number(node(), '00000')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
运行以下,前提是input.xml
包含你的XML:
$ xsltproc transform.xml input.xml
不安全的解决方案:
这些依赖于开始标签 <SpecialTag>
和结束标签 </SpecialTag>
在同一行并且每行只有一个。
下面的解决方案仅被提及,因为作者明确地用sed/bash/shell标记了问题。这些不是完成工作的正确工具!
它们都使用正则表达式来捕获 <SpecialTag>
,然后是几个数字,然后是 </SpecialTag>
并用这些数字的 0 填充版本转换捕获的数字。
使用 sed:
sed --regexp-extended 's@<SpecialTag>([0-9]+)</SpecialTag>@<SpecialTag>0000000</SpecialTag>@;s@0*([0-9]{5,})@@'
使用 perl:
perl -pe 's@<SpecialTag>([0-9]+)</SpecialTag>@sprintf("<SpecialTag>%05d</SpecialTag>",)@e'
使用 awk:
awk '{gsub( /<SpecialTag>[0-9]+<\/SpecialTag>/, sprintf("<SpecialTag>%05d</SpecialTag>", gensub(/[^0-9]/, "","g"))); print}'