如何在xmlstarlet的concat中打印换行符

时间:2017-03-10 21:45:57

标签: xml xpath xmlstarlet

我想用换行符分隔输出,所以我尝试了:

xmlstarlet sel -t -m "//node01" -v 'concat(@title,"\n",script/code)' -n input.xml

但是,打印的是“\ n”文字值,输出在同一行。如何在concat()函数中强制换行?

示例input.xml是:

<test>
<aaa>This is a test</aaa>
<node01 title="howdy">
    <script>
        <code>function idoit() {
                console.log("hello world");
            }
        </code>
    </script>
</node01>
</test>

当我跑步时,输出是:

howdy\nfunction idoit() {
                console.log("hello world");
            }

1 个答案:

答案 0 :(得分:2)

您可以在参数中提供文字换行符,其语法是特定于shell的。

  • 这适用于任何POSIX sh

    xmlstarlet sel -t -m "//node01" -v 'concat(@title,"
    ",script/code)' -n input.xml
    
  • 在bash中,您可以使用$'ANSI C Quoting'

    xmlstarlet sel -t -m "//node01" -v $'concat(@title,"\n",script/code)' -n input.xml
    

另一个不特定于shell的技巧是使用换行符设置XPATH变量作为值:

xmlstarlet sel -t --var nl -n -b -m "//node01" -v 'concat(@title,$nl,script/code)' -n input.xml