是否有用于可控 XML 格式设置的样式表或 Windows 命令行工具,特别是每行一个属性?
我正在寻找适用于 Windows 的 XSLT 或命令行工具(或可以制作成命令行工具的 C# 代码等),以执行 XML 漂亮打印。具体来说,我想要一个能够将属性一对一放置的能力,例如:
<Node>
<ChildNode
value1='5'
value2='6'
value3='happy' />
</Node>
它不必完全像那样,但我想将它用于具有数十个节点的 XML 文件。属性并将它们分布在多行中使它们更易于阅读、编辑和文本比较。
注意:我认为我的首选解决方案是可以通过 C# 方法传递的 XSLT 表,尽管 Windows 命令行工具也很好。
I am searching for an XSLT or command-line tool (or C# code that can be made into a command-line tool, etc) for Windows that will do XML pretty-printing. Specifically, I want one that has the ability to put attributes one-to-a-line, something like:
<Node>
<ChildNode
value1='5'
value2='6'
value3='happy' />
</Node>
It doesn't have to be EXACTLY like that, but I want to use it for an XML file that has nodes with dozens of attributes and spreading them across multiple lines makes them easier to read, edit, and text-diff.
NOTE: I think my preferred solution is an XSLT sheet I can pass through a C# method, though a Windows command-line tool is good too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个用于执行此操作的 PowerShell 脚本。它接受以下输入:
...并将其生成为输出:
开始吧:
获取该脚本,将其另存为 C:\formatxml.ps1。然后,从 PowerShell 提示符中键入以下内容:
该脚本基本上仅使用 .NET 框架,因此您可以非常轻松地将其迁移到 C# 应用程序中。
我希望这对你有用。
Here's a PowerShell script to do it. It takes the following input:
...and produces this as output:
Here you go:
Take that script, save it as C:\formatxml.ps1. Then, from a PowerShell prompt type the following:
This script is basically just using the .NET framework so you could very easily migrate this into a C# application.
I hope that's useful to you.
下面是一个小型 C# 示例,您的代码可以直接使用它,也可以将其内置到 exe 中并在命令行中调用“
myexe from.xml to.xml
”:示例输入:
示例输出(请注意,您可以使用
settings.OmitXmlDeclaration
删除):
请注意,如果您想要一个字符串而不是编写到一个文件,只需与
StringBuilder
交换:Here's a small C# sample, which can be used directly by your code, or built into an exe and called at the comand-line as "
myexe from.xml to.xml
":Sample input:
Sample output (note you can remove the
<?xml ...
withsettings.OmitXmlDeclaration
):Note that if you want a string rather than write to a file, just swap with
StringBuilder
:在 SourceForge 上尝试 Tidy。虽然它经常在 [X]HTML 上使用,但我之前已经在 XML 上成功使用过 - 只要确保使用
-xml
选项即可。http://tidy.sourceforge.net/#docs
人们已经将其移植到多个平台,并且它可以作为可执行和可调用库使用。
Tidy 有一堆选项,包括:
http://api.html-tidy.org/tidy/quickref_5.0.0.html#indent
一个警告:
但我怀疑除非您的 XML 非常先进,否则该工具应该可以正常工作。
Try Tidy over on SourceForge. Although its often used on [X]HTML, I've used it successfully on XML before - just make sure you use the
-xml
option.http://tidy.sourceforge.net/#docs
People have ported to several platforms and it available as an executable and callable library.
Tidy has a heap of options including:
http://api.html-tidy.org/tidy/quickref_5.0.0.html#indent
One caveat:
But I suspect unless your XML is really advanced, the tool should work fine.
有一个工具可以将属性拆分为每行一个:xmlpp。它是一个 Perl 脚本,因此您必须安装 perl。用法:
您还可以通过创建名为 attributeOrdering.txt 的文件并调用 perl xmlpp.pl -s -t input.xml 来确定属性的顺序。要获得更多选项,请使用 perl xmlpp.pl -h
我希望它没有太多错误,但到目前为止它对我有用。
There is a tool, that can split attributes to one per line: xmlpp. It's a perl script, so you'll have to install perl. Usage:
You can also determine the ordering of attributes by creating a file called attributeOrdering.txt, and calling
perl xmlpp.pl -s -t input.xml
. For more options, useperl xmlpp.pl -h
I hope, it doesn't have too many bugs, but it has worked for me so far.
XML Notepad 2007 可以手动执行此操作...让我看看是否可以编写脚本。
不……它可以像这样启动它:
剩下的只需单击“保存”按钮即可。 Power Shell,其他工具可以自动执行此操作。
XML Notepad 2007 can do so manually ... let me see if it can be scripted.
Nope ... it can launch it like so:
The rest is just clicking the save button. Power Shell, other tools can automate that.
只需使用此 xslt:
或者,作为另一种选择,这里是一个 perl 脚本: http://software.decisionsoft .com/index.html
Just use this xslt:
Or, as another option, here is a perl script: http://software.decisionsoft.com/index.html
您可以实现一个简单的 SAX 应用程序,它将按您喜欢的方式复制所有内容并缩进属性。
UPD:
SAX 代表
Simple API for XML
。它是XML解析的推送模型(Builder设计模式的经典示例)。该 API 存在于大多数当前的开发平台中(尽管本机 .Net 类库缺少一个,但有 XMLReader)这是 python 中的原始实现,它相当神秘,但您可以实现主要思想。
You can implement a simple SAX application that will copy everything
as is
and indent attributes how you like.UPD:
SAX stands for
Simple API for XML
. It is a push model of XML parsing (a classical example of Builder design pattern). The API is present in most of the current development platforms (though native .Net class library lacks one, having XMLReader intead)Here is a raw implementation in python, it is rather cryptic but you can realize the main idea.