在属性之间添加换行符 [英] Adding line breaks between attributes

查看:49
本文介绍了在属性之间添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,在通过我的 XSLT 发送后,XML 属性前不再有换行符.所以例如

I have an XML document which after sending it through my XSLT no longer has line breaks before the XML attributes. So for example

<myoutertag one="a"
            two="b"
            three="c">
    <myinnertag four="d"
                five="e"/>
</myoutertag>

会变成

<myoutertag one="a" two="b" three="c">
    <myinnertag four="d" five="e"/>
</myoutertag>

这当然是完全有效的 XML,但它更难阅读,尤其是在有很多长属性值的情况下.据我所知,XSLT 无法保留这些换行符,因为 XSLT 处理器没有传递这样的不重要信息.

This is of course perfectly valid XML but it's more difficult to read, especially if there are many long attribute values. From what I've read, XSLT is not able to preserve these line breaks as the XSLT processor is not passed such unimportant information.

所以,我现在正在寻找的是基于命令行的漂亮打印机(可在 Linux 中使用),理想情况下它只会更改文档,因为它会在属性之间添加换行符.是否在第一个属性之前添加一个对我来说几乎无关紧要,只要它更容易阅读即可.

So, what I'm looking for now is a command line based pretty printer (usable in Linux) which ideally would only change the document in that it adds line breaks between the attributes. Whether it adds one before the first attribute or not is pretty much irrelevant to me, just as long as it's more easily readable.

我正在使用输入文件

<?xml version="1.0" encoding="UTF-8"?>

<myoutertag one="a" two="b" three="c">
    <myinnertag four="d" five="e"/>
</myoutertag>

xmllint --format

我尝试了 xmllint --format test.xmlcat test.xml |xmllint --format - 具有相同的结果:

xmllint --format

I tried both xmllint --format test.xml and cat test.xml | xmllint --format - with the same result:

<?xml version="1.0" encoding="UTF-8"?>
<myoutertag one="a" two="b" three="c">
  <myinnertag four="d" five="e"/>
</myoutertag>

所以,变化是:

  • xml 偏角消失后的换行符
  • 的缩进从四个空格减少到两个空格
  • the line break after the xml declination is gone
  • the indentation of <myinnertag> was reduced from four spaces to two spaces

这些变化我都不想要.这是使用 libxml 版本 20706.

I want neither of those changes. This is using libxml version 20706.

我尝试了nonensgmlsniceindentedrecord> 和 record_c.唯一接近的是 nsgmls 它将添加换行符,但结果如下所示:

I tried the styles none, nsgmls, nice, indented, record and record_c. The only one that comes close is nsgmls which will add line breaks, but the result looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<myoutertag
one="a"
two="b"
three="c"
><myinnertag
four="d"
five="e"
/></myoutertag>

所以,没有缩进和奇怪的换行.

So, no indentation and weird line breaking.

xmlstarter fo test.xml 的输出与 xmllint 的输出相同.我也尝试找到类似 xmlstarter -ed -P --insert "//@*" -t text -n "" -v "\\n" test.xml 的东西,但这导致了 glibc指针错误.我想这并不奇怪,因为我正在尝试在属性之间添加文本.

The output of xmlstarter fo test.xml is the same as with xmllint. I also tried finding something like xmlstarter -ed -P --insert "//@*" -t text -n "" -v "\\n" test.xml but that resulted in a glibc pointer error. Not surprising I guess, as I'm trying to add text in between attributes.

这是我迄今为止最接近的一次.运行命令 tidy -quiet -xml -indent -wrap 1 test.xml 给我:

This is the closest I've gotten so far. Running the command tidy -quiet -xml -indent -wrap 1 test.xml gives me:

<?xml version="1.0"
encoding="UTF-8"?>
<myoutertag one="a"
two="b"
three="c">

  <myinnertag four="d"
  five="e"/>
</myoutertag>

所以,如果我能让它在新行中的那些属性之前缩进一些,基本上可以解决我的问题(我认为).

So, if I could get it to indent some more before those attributes in new lines that would basically solve my problem (I think).

还有什么建议吗?

推荐答案

好的,我找到了解决方案.我使用的工具叫做 HTML Tidy(嗯,实际上我使用了 jTidy,HTML Tidy 到 Java 的端口,因此是可移植的).该工具提供了许多配置选项;我要找的那个叫做 indent-attributes: true.其实我的整个配置文件是:

OK, I've found a solution. The tool I used is called HTML Tidy (well, actually I used jTidy, a port of HTML Tidy to Java which therefore is portable). The tool offers many options for configuration; the one I was looking for is called indent-attributes: true. In fact, my whole configuration file is:

add-xml-decl: true
drop-empty-paras: false
fix-backslash: false
fix-bad-comments: false
fix-uri: false
input-xml: true
join-styles: false
literal-attributes: true
lower-literals: false
output-xml: true
preserve-entities: true
quote-ampersand: false
quote-marks: false
quote-nbsp: false

indent: auto
indent-attributes: true
indent-spaces: 4
tab-size: 4
vertical-space: true
wrap: 150

char-encoding: utf8
input-encoding: utf8
newline: CRLF
output-encoding: utf8

quiet: true

这些选项的含义在 Tidy 手册(或 man如果你在 Linux 系统上安装它的页面),我最关心的是我可以设置缩进设置的中间块.

The meanings of those options are explained in the Tidy manual (or the man page if you install it on a Linux system), I mostly cared about that middle block where I can set the indentation settings.

我现在可以使用命令 java -jar jtidy-r938.jar -config tidy.config test.xml 调用该工具,输出将是

I can now call the tool using the command java -jar jtidy-r938.jar -config tidy.config test.xml and the output will be

<?xml
  version="1.0"
  encoding="UTF-8"?>
<myoutertag
 one="a"
 two="b"
 three="c">
    <myinnertag
     four="d"
     five="e" />
</myoutertag>

现在我很高兴.:-)

这篇关于在属性之间添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆