使用 xslt 将不带分隔符的文本文件转换为 xml
Transform text file without delimiter with xslt to xml
我正在寻找合适的工具将文本文件转换为 xml。
文本文件如下所示:
2017-01-03-10.11.1201000B H4_01DE33411121...
2017-01-01-09.12.1301000BHAX4_01DE34256137...
2017-01-01-10.12.1301000BMLH4_01DE63789221...
每一行都是一个实体的内容,我有以下信息:
Letter 0-18: Attribute1
Letter 19-21: Attribute2
Letter 22-23: Attribute3
Letter 24: Attribute4
Letter 25-31: Attribute5
and so on....
等等...
现在我正在寻找一种工具,它可以根据此规则将此文本文件转换为以下 xml
<entities>
<entity>
<attribute1>2017-01-03-10.11.12</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>H4_01</attribute5>
... and so on
</entity>
<entity>
<attribute1>2017-01-01-09.12.13</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>HAX4_01</attribute5>
... and so on
</entity>
<entity>
<attribute1>2017-01-01-10.12.13</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>MLH4_01</attribute5>
... and so on
</entity>
</entities>
该工具还需要实现一些简单的逻辑,例如裁剪字符串,if/else,日期格式转换。
首先,我想到了使用 xslt - 这样这个奇怪的文本文件的所有者甚至可以自己生成相应的配置文件(最好!)。但是我经常看到xslt只是用来把xml转成其他格式,而不是把纯文本文件转成xml.
它也应该是可维护的,所以使用 awk 和 sed 的 shell 脚本会非常混乱。
你知道比xslt更合适的工具吗?
一个聪明的方法是从描述输入的数据描述文件生成 XSLT 样式表。
如果数据描述文件有
<fields>
<field name="attribute1" start="1" length="18"/>
<field name="attribute2" start="19" length="2"/>
</fields>
然后很容易生成 XSLT 3.0 转换
<xsl:template name="main">
<entities>
<xsl:for-each select="unparsed-text-lines('input.xml')">
<entity>
<attribute1>{substring(., 1, 18)}</attribute1>
<attribute2>{substring(., 1, 18)}</attribute2>
</entity>
</xsl:for-each>
</entities>
</xsl:template>
(生成 XSLT 2.0 只是稍微复杂一点,但是生成 XSLT 1.0 更难,因为您无法直接读取纯文本文件)。
实施您的 "simple logic" 有点棘手,但向数据描述添加额外字段并不难:
<field name="attribute1" start="1" length="18" action="checkDate"/>
这导致生成的 XSLT 为
<attribute1>{f:checkDate(substring(., 1, 18))}</attribute1>
调用样式表中的函数,例如
<xsl:function name="f:checkDate" as="xs:string">
<xsl:param name="in" as="xs:string"/>
<xsl:sequence select="if ($in castable as xs:date) then $in else error(...)"/>
</xsl:function>
我正在寻找合适的工具将文本文件转换为 xml。
文本文件如下所示:
2017-01-03-10.11.1201000B H4_01DE33411121...
2017-01-01-09.12.1301000BHAX4_01DE34256137...
2017-01-01-10.12.1301000BMLH4_01DE63789221...
每一行都是一个实体的内容,我有以下信息:
Letter 0-18: Attribute1
Letter 19-21: Attribute2
Letter 22-23: Attribute3
Letter 24: Attribute4
Letter 25-31: Attribute5
and so on....
等等...
现在我正在寻找一种工具,它可以根据此规则将此文本文件转换为以下 xml
<entities>
<entity>
<attribute1>2017-01-03-10.11.12</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>H4_01</attribute5>
... and so on
</entity>
<entity>
<attribute1>2017-01-01-09.12.13</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>HAX4_01</attribute5>
... and so on
</entity>
<entity>
<attribute1>2017-01-01-10.12.13</attribute1>
<attribute2>010</attribute2>
<attribute3>00</attribute3>
<attribute4>B</attribute4>
<attribute5>MLH4_01</attribute5>
... and so on
</entity>
</entities>
该工具还需要实现一些简单的逻辑,例如裁剪字符串,if/else,日期格式转换。
首先,我想到了使用 xslt - 这样这个奇怪的文本文件的所有者甚至可以自己生成相应的配置文件(最好!)。但是我经常看到xslt只是用来把xml转成其他格式,而不是把纯文本文件转成xml.
它也应该是可维护的,所以使用 awk 和 sed 的 shell 脚本会非常混乱。
你知道比xslt更合适的工具吗?
一个聪明的方法是从描述输入的数据描述文件生成 XSLT 样式表。
如果数据描述文件有
<fields>
<field name="attribute1" start="1" length="18"/>
<field name="attribute2" start="19" length="2"/>
</fields>
然后很容易生成 XSLT 3.0 转换
<xsl:template name="main">
<entities>
<xsl:for-each select="unparsed-text-lines('input.xml')">
<entity>
<attribute1>{substring(., 1, 18)}</attribute1>
<attribute2>{substring(., 1, 18)}</attribute2>
</entity>
</xsl:for-each>
</entities>
</xsl:template>
(生成 XSLT 2.0 只是稍微复杂一点,但是生成 XSLT 1.0 更难,因为您无法直接读取纯文本文件)。
实施您的 "simple logic" 有点棘手,但向数据描述添加额外字段并不难:
<field name="attribute1" start="1" length="18" action="checkDate"/>
这导致生成的 XSLT 为
<attribute1>{f:checkDate(substring(., 1, 18))}</attribute1>
调用样式表中的函数,例如
<xsl:function name="f:checkDate" as="xs:string">
<xsl:param name="in" as="xs:string"/>
<xsl:sequence select="if ($in castable as xs:date) then $in else error(...)"/>
</xsl:function>