2017-03-10 80 views
1

我想我有一個換行符輸出分離的新行內CONCAT,所以我嘗試:如何打印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"); 
      } 
+0

當你嘗試'concat(@title,「 」,script/code)'時會發生什麼?是不是xmlstarlet只是簡單地從你的輸入中創建一些XSLT,然後字符引用可以工作? –

+0

嗨馬丁,不錯的提示,但我試過它不起作用。 – ifelsemonkey

回答

1

你可以給在參數文字換行,這樣做的語法是殼-具體。

  • 這應該在任何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 
    

另一個竅門未殼具體是使用換行符將值設置爲XPATH變量:

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

謝謝npostavs。 – ifelsemonkey