2016-03-20 7 views
1

xmlstartletでplist XMLファイルを照会したいとします。私はstringタグのテキストをユニークなkeyタグの後に探しています。 XMLファイルには、私は、例えば後の方法に文字列値を探していますどのような場合でもplistファイルでは、xmlstarletツールを使用して一意のキータグの後に文字列を抽出する方法

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>BuildMachineOSBuild</key> 
    <string>15B42</string> 
    <key>CFBundleDevelopmentRegion</key> 
    <string>en</string> 
    <key>CFBundleIconFile</key> 
    <string>AppIcon</string> 
    . 
    . 
    . 
</dict> 
</plist> 

または

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<othertag> 
    <dict> 
     <key>BuildMachineOSBuild</key> 
     <string>15B42</string> 
     <key>CFBundleDevelopmentRegion</key> 
     <string>en</string> 
     <key>CFBundleIconFile</key> 
     <string>AppIcon</string> 
     . 
     . 
     . 
    </dict> 
</othertag> 
</plist> 

のように見えることができますキーCFBundleDevelopmentRegion(この場合はen)。

ディクテーションが発生する階層内の場所は不明です(/plist/dictまたは/plist/another/dictなど)が、ファイル全体で一意です。

私は

xmlstarlet sel -t -v '//string/following::key[text()="CFBundleDevelopmentRegion"]' myfile.plist 

を試してみましたが、私は任意の出力を得ることはありません。私のXPathが間違っているのですか、またはxmlstarletに他のパラメータを指定する必要がありますか?

答えて

2

あなたがこの方法を試すことができます。

//key[.='CFBundleDevelopmentRegion']/following-sibling::string[1] 

XPathは、どこでもコンテンツが"CFBundleDevelopmentRegion"に等しいXMLドキュメントで、key要素を見つけ、その後、最寄り次-兄弟string要素を返します。

のでxmlstarletとの完全なコマンドラインは次のようになります。

xmlstarlet sel --net -t -v '//key[.="CFBundleShortVersionString"]/following-sibling::string[1]' myfile.plist 
+1

完全にこの作品! – halloleo

関連する問題