本文介绍了XSLT:CSV(或平面文件或纯文本)到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用XSLT将纯文本文件转换为XML文件。我开始使用CSV文件,因为这是一个众所周知的文件格式,我可以开始Googling的例子。



我偶然发现了:,其中还指向。



这些链接包含是一个XSLT(2.0),可以采取CSV文件并将其转换为XML文件。



...除非它实际上不工作。 / p>

我在我的Maven Eclipse项目中设置它,下载了最新的Saxon依赖(9.4 HE)并试图使用它。我遇到此错误:

当它开始解析文件时,它命中第一个字符,发现它不是一个< 字符,自称这不是一个XML文件! yew tee eff,mate!并炸毁。哪种类型的运行与想法相反,这个XSLT应该工作在非XML文件(即,CSV文件)。强制您将非XML包装在XML标签中,以使其完全无法工作。



刚开始我认为问题是我没有使用Saxon jar直接在命令行上就像例子。所以我做到了。结果是相当熟悉的:

使用较新的版本,我需要回去使用示例最初写的版本。所以我回到了SaxonB 9.1.0.8,并在Eclipse和命令行上试过。

我发现如果我将整个(例如< whatever> item1,item2,item3< / whatever> )中的CSV文件的内容开始几乎工作过去的第一个字符,我开始得到一个不同的错误在过程中更远)。



那么为什么地狱不是这个XSLT工作?为什么博客它的帖子(和所有附带的评论部分的附带评论)似乎表明它?我还发现它引用了,和作为接受的答案。这怎么可能?它不起作用!



因此,整个互联网上的每个人都在彼此和/或他们自己在一个巨大的阴谋,激怒我,或有一些非常简单,完整的步骤我只是缺少,是需要使Saxon使用XSLT将CSV文件转换为XML文件。



所以,任何人知道它是?



编辑:接受pgfearo的回答。此修改部分的原始内容现在是自己的问题:



编辑2:如果有人对我的XSLT喜欢,最终在一个不同的问题在这里:

解决方案

我不认为这是一个阴谋 - 你没有包括你使用的Saxon命令行,但我怀疑你调用csv .csv作为变换的源。因为这不是一个XML文件,你会得到一个XML解析器错误,如你所示。



你引用的XSLT样式表有一个名为'main' ,请使用命令行上的 -it 选项将'main'设置为初始模板。使用此集合,您现在不需要为转换提供源。 Saxon命令行选项已记录在。


I am trying to convert plain text files to XML files using XSLT. I started with CSV files, because that is a well-known file format that I could start Googling examples on.

I stumbled onto this: http://ajwelch.blogspot.com/2007/02/csv-to-xml-converter-in-xslt-20.html, which also points at http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html.

Those links contain what is, supposedly, an XSLT (2.0) that can take a CSV file and convert it to an XML file.

...Except it doesn't actually work.

I set it up in my Maven Eclipse project, downloaded the latest Saxon dependency (9.4 HE) and tried to use it. I was met with this error:

That seems to indicate to me that when it began parsing the file, it hit the first character, found it wasn't a < character, exclaimed to itself "This isn't an XML file! Double-yew tee eff, mate!" and blew up. Which kind of runs contrary to idea that this XSLT is supposed to work on files that are not XML (namely, CSV files instead). Forcing you to wrap your non-XML in an XML tag to have it work completely defeats the purpose.

At first I thought maybe the problem was that I wasn't using the Saxon jar directly on the command line like the example. So I did just that. The result was something quite familiar:

I thought that perhaps since I was using a newer version, I needed to go back and use the version that the example was originally written under. So I went back to SaxonB 9.1.0.8 and tried it both in Eclipse and on the command line. Care to guess what happened?

I discovered that if I wrap the entire contents of the CSV file in a dummy xml tag (e.g. <whatever>item1,item2,item3</whatever>) it starts to almost work (it at least makes it past the first character and I start to get a different error farther along in the process).

So why the hell doesn't this XSLT work? Why does the blog its posted on (and all of the attendant comments in the attached comment section) seem to indicate that it does? I also found it referenced here in the Ubuntu help documentation, and as the accepted answer on this StackOverflow question. How is that possible? It doesn't work!

So either everyone on the entire Internet is lying to each other and/or themselves in a giant conspiracy designed to enrage me, or there is some very simple, integral step I am just missing that is required to make Saxon use that XSLT to convert a CSV file to an XML file.

So, anybody know which it is?

Edit: pgfearo's answer accepted. The original contents of this "Edit" section is now it's own question here: Saxon in Java: XSLT for CSV to XML

Edit 2: If anyone is curious as to what my XSLT ended up looking like, that ended up in a different question here: XSLT remove() function

解决方案

I don't think it's a conspiracy - you haven't included the Saxon command line you used but I suspect you're calling the transform with csv.csv as the source of the transform. Because this isn't an XML file you will get an XML parser error such as you've shown.

The XSLT stylesheet you reference has an entry template called 'main', use the -it option on the command line to set 'main' as the initial template. With this set, you now don't need to supply a source for the transform. The Saxon command line options are documented here.

这篇关于XSLT:CSV(或平面文件或纯文本)到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:23