2

xmlスニペットは次のとおりです。

$ cat short.xml 
<hostnames>
    <hostname name="yahoo.com" type="user"/>
    <hostname name="ir1.fp.vip.sp2.yahoo.com" type="PTR"/>
</hostnames>
<hostnames>
    <hostname name="Inc.com" type="user"/>
    <hostname name="www.inc.com" type="PTR"/>
</hostnames>

必要な出力は次のとおりです。

yahoo.com | ir1.fp.vip.sp2.yahoo.com
Inc.com | www.inc.com

これまでのところ、部分的にしか機能していないもの:$ xml sel -t -m "// hostname" -v "@name" -n short.xml

Type=条件を適切にトラップできないようです。TIA。

4

3 に答える 3

4

xmlstarletを1回だけ使用するさらに2つのソリューション(繰り返す必要はありません):

xmlstr='
<root>
  <hostnames>
    <hostname name="yahoo.com" type="user"/>
    <hostname name="ir1.fp.vip.sp2.yahoo.com" type="PTR"/>
  </hostnames>
  <hostnames>
    <hostname name="Inc.com" type="user"/>
    <hostname name="www.inc.com" type="PTR"/>
  </hostnames>
</root>
'

echo "$xmlstr" | xmlstarlet sel -T -t -m "//hostnames" -m "hostname[@type='user']" -v '@name' -o " | " -b -m "hostname[@type='PTR']" -v '@name' -n

echo "$xmlstr" | xmlstarlet sel -T -t -m "//hostname" -i "@type='user'" -v '@name' -o " | " -b -i "@type='PTR'" -v '@name' -n
于 2011-03-15T13:31:52.230 に答える
3

xmlstarlet elまたは何かでホスト名を数え、次に次のようなもので繰り返す必要があります。

xmlstarlet sel -t -c "//hostnames[1]" short.xml | xmlstarlet sel -t -m "//hostname/@name" -v . -o ' | '

XMLがより適切に設計されていれば、これははるかに簡単です。:)

于 2011-02-04T23:10:52.537 に答える
0

問題の例は無効なxmlです。

xmlstarlet --version
1.3.1
compiled against libxml2 2.8.0, linked with 20800
compiled against libxslt 1.1.26, linked with 10126
xmlstarlet val -e short.xml 
short.xml:5.1: Extra content at the end of the document
<hostnames>
^
short.xml - invalid

mitmによる回答のアイデアは、非常に優れた治療法です。

于 2013-01-16T13:19:53.793 に答える