我接受转换为标准,<tag></tag>但<tag/>客户希望它采用前一种布局,而不是后者。我查看了各种选项xmllint,但无法弄清楚需要什么。
或者,除此之外还有其他xmllint可以使用的吗?
小智 1
xmllint具有可用于产生所需输出的选项。来自帮助:
Run Code Online (Sandbox Code Playgroud)--c14n : save in W3C canonical format v1.0 (with comments) --c14n11 : save in W3C canonical format v1.1 (with comments) --exc-c14n : save in W3C exclusive canonical format (with comments)
使用其中任何一个都可以
xmllint --c14n test.xml
Run Code Online (Sandbox Code Playgroud)
不幸的是,在一个调用中组合多个格式化选项xmllint似乎不起作用,因此,如果您还想使用--format重新xmllint格式化/重新缩进输出,则需要将第一个xmllint调用的输出通过管道传输到第二个调用。执行此操作时,似乎xmllint需要将-stdin 重定向放在命令末尾。
例子:
对于test.xml作为
<?xml version="1.0" encoding="UTF-8"?>
<outer><inner/></outer>
Run Code Online (Sandbox Code Playgroud)
命令
xmllint --format test.xml | xmllint --c14n -
Run Code Online (Sandbox Code Playgroud)
产生输出
<outer>
<inner></inner>
</outer>
Run Code Online (Sandbox Code Playgroud)
笔记:
--format选项会再次折叠展开的空标签,因此管道中选项的顺序很重要