使用 XSLT 将常规文本文件转换为 XML [英] Regular text file to XML using XSLT
问题描述
我有一个看起来像这样的文本文件:
I have a text file which looks like that:
XXX^YYYY^AAAAA^XXXXXX^AAAAAA....
字段使用插入符号 (^) 分隔,我的假设是:
Fields are separated using a caret(^), my presumptions are:
第一个字段 = NAME
第二个字段 = 姓氏
第三个字段 = 地址
the first field = NAME
the second filed = Last name
third field = Address
等等.
我想使用 xsl (XSLT) 将其转换为有效的 XML.例如:
I would like to turn it into a valid XML using xsl (XSLT). such as:
<name>XXX</name>
<l_name>YYYY</l_name>
我知道使用 Perl 可以轻松完成,但如果可能,我需要使用 XSLT 来完成.
I know It can be done easily with Perl, but I need to do it with XSLT, if possible.
推荐答案
可以使用标准 XSLT 2.0 函数读取文本(非 XML)文件 unparsed-text()
.
然后可以使用标准的 XPath 2.0 函数 tokenize()代码>
和另外两个标准 XPath 2.0 函数接受常规一个表达式作为它们的参数之一 -- match()
和 replace()
.
Then one can use the standard XPath 2.0 function tokenize()
and two other standard XPath 2.0 functions that accept regular a expression as one of their arguments -- matches()
and replace()
.
XSLT 2.0 有自己强大的使用正则表达式处理文本的指令::
,<xsl:matching-substring>
和
指令.
XSLT 2.0 has its own powerful instructions to handle text processing using regular expressions:: the <xsl:analyze-string>
, the <xsl:matching-substring>
and the <xsl:non-matching-substring>
instruction.
在这个实际示例中,通过这些函数和指令查看 XSLT 文本处理的一些更强大的功能:一个 XSLT WideFinder 问题的解决方案.
See some of the more powerful capabilities of XSLT text processing with these functions and instructions in this real-world example: an XSLT solution to the WideFinder problem.
最后,这是一个 XSLT 1.0 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" exclude-result-prefixes="ext my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<my:fieldNames>
<name>FirstName</name>
<name>LastName</name>
<name>City</name>
<name>State</name>
<name>Zip</name>
</my:fieldNames>
<xsl:variable name="vfieldNames" select=
"document('')/*/my:fieldNames"/>
<xsl:template match="/">
<xsl:variable name="vrtfTokens">
<xsl:apply-templates/>
</xsl:variable>
<xsl:variable name="vTokens" select=
"ext:node-set($vrtfTokens)"/>
<results>
<xsl:apply-templates select="$vTokens/*"/>
</results>
</xsl:template>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:variable name="vWord" select=
"substring-before(concat($pText, '^'),'^')"/>
<word>
<xsl:value-of select="$vWord"/>
</word>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,'^')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="word">
<xsl:variable name="vPos" select="position()"/>
<field>
<xsl:element name="{$vfieldNames/*[position()=$vPos]}">
</xsl:element>
<value><xsl:value-of select="."/></value>
</field>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<t>John^Smith^Bellevue^WA^98004</t>
产生想要的、正确的结果:
<results>
<field>
<FirstName/>
<value>John</value>
</field>
<field>
<LastName/>
<value>Smith</value>
</field>
<field>
<City/>
<value>Bellevue</value>
</field>
<field>
<State/>
<value>WA</value>
</field>
<field>
<Zip/>
<value>98004</value>
</field>
</results>
这篇关于使用 XSLT 将常规文本文件转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!