2016-03-20 17 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會發現key元素的XML文檔,其中的內容等於"CFBundleDevelopmentRegion"中的任何位置,然後返回最近以下同胞string元素。

因此,與xmlstarlet完整的命令行看起來是這樣的:

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

這項工作完美! – halloleo

相關問題