如何从命令行部分地打印XML文件?

时间:2015-11-05 22:55:11

标签: xml shell formatting xmlstarlet xmllint

我正在编写一个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元素的唯一更改是换行符 在它之前。

我在xmllintxmlstarlet中找不到任何选项来忽略a 某些因素。有没有什么工具可以帮我实现这个目标?我上线了 Linux,如果重要的话。

1 个答案:

答案 0 :(得分:1)

当需要内部元素jelly:脚本时,空格可能没有变化,那么你可以使用xml_pp(在安装了perl包perl-XML-Twig的linux上。)选项{ {1}}可用于保留这些元素中的所有空格:

-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