如何从命令行部分漂亮地打印 XML 文件?
How to partially pretty print XML files from the command line?
我正在编写一个 unix shell 脚本,我需要在其中打印 XML 文件,
但要注意的是,其中有些部分我可能无法触及。
也就是说,它们是 Apache Jelly 脚本,包含在 XML
我需要漂亮打印的文件。所以我需要转换这个
<proc source="customer"><scriptParam value="_user"/><scriptText><jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText></proc>
进入这个
<proc source="customer">
<scriptParam value="_user"/>
<scriptText>
<jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText>
</proc>
请注意 jelly:script
元素的唯一变化是换行符
在它之前。
我在 xmllint
或 xmlstarlet
中找不到任何选项来忽略
一定的元素。有什么工具可以帮助我实现这一目标吗?我上线了
Linux,如果重要的话。
当要求里面元素jelly:script不能改变空格时,可以使用xml_pp
(在linux上安装perl 包 perl-XML-Twig
。选项 -p some-element
可用于保留这些元素内的所有空白:
xml_pp -p jelly:script thefile.xml
这将创建这个:
<proc source="customer">
<scriptParam value="_user"/>
<scriptText>
<jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText>
</proc>
如您所见,开始元素 <jelly:script>
也是缩进的,因为添加的空格仍在元素之外。
如果这也被禁止,那么您必须选择更高的级别 (scriptText
),或者将其通过管道传递给再次删除这些空格的命令:
xml_pp -p jelly:script thefile.xml | perl -pe 's/^\s*(<jelly:script>)//'
我正在编写一个 unix shell 脚本,我需要在其中打印 XML 文件, 但要注意的是,其中有些部分我可能无法触及。 也就是说,它们是 Apache Jelly 脚本,包含在 XML 我需要漂亮打印的文件。所以我需要转换这个
<proc source="customer"><scriptParam value="_user"/><scriptText><jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText></proc>
进入这个
<proc source="customer">
<scriptParam value="_user"/>
<scriptText>
<jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText>
</proc>
请注意 jelly:script
元素的唯一变化是换行符
在它之前。
我在 xmllint
或 xmlstarlet
中找不到任何选项来忽略
一定的元素。有什么工具可以帮助我实现这一目标吗?我上线了
Linux,如果重要的话。
当要求里面元素jelly:script不能改变空格时,可以使用xml_pp
(在linux上安装perl 包 perl-XML-Twig
。选项 -p some-element
可用于保留这些元素内的所有空白:
xml_pp -p jelly:script thefile.xml
这将创建这个:
<proc source="customer">
<scriptParam value="_user"/>
<scriptText>
<jelly:script>
<jelly:log level="info">
this text needs
to keep its indent level
and this is none of my business
</jelly:log>
<!-- get date -->
<sql:query var="rs"><![CDATA[
select sysdate
from dual
]]></sql:query>
</jelly:script>
</scriptText>
</proc>
如您所见,开始元素 <jelly:script>
也是缩进的,因为添加的空格仍在元素之外。
如果这也被禁止,那么您必须选择更高的级别 (scriptText
),或者将其通过管道传递给再次删除这些空格的命令:
xml_pp -p jelly:script thefile.xml | perl -pe 's/^\s*(<jelly:script>)//'