首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >info.plist - xmlstarlet不返回指定的节点

info.plist - xmlstarlet不返回指定的节点
EN

Stack Overflow用户
提问于 2018-01-23 00:52:36
回答 2查看 338关注 0票数 0

我正在尝试使用xmlstartlet解析info.plist (针对iOS应用程序)。我是在MacBook专业版上用OSX做这件事的。这是我的info.plist:

代码语言:javascript
复制
<?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>UIRequiresFullScreen</key>
    <true/>
    <key>ITSAppUsesNonExemptEncryption</key>
    <false/>
    <key>UIDeviceFamily</key>
    <array>
        <integer>1</integer>
        <integer>2</integer>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>MinimumOSVersion</key>
    <string>9.3</string>
    <key>CFBundleDisplayName</key>
    <string>My iOS App</string>
    <key>CFBundleIdentifier</key>
    <string>local.nowhere.MyiOSApp</string>
    <key>CFBundleShortVersionString</key>
    <string>3.3.0</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>UISupportedExternalAccessoryProtocols</key>
    <key>UIBackgroundModes</key>
    <array>
        <string>external-accessory</string>
    </array>
    <key>UIAppFonts</key>
    <array>
        <string>FontAwesome.ttf</string>
    </array>
    <key>CFBundleVersion</key>
    <string>321</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <array>
        <string>Location enabled?</string>
    </array>
    <key>NSCameraUsageDescription</key>
    <string>needs access to the camera</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>needs access to photos</string>
    <key>NSCalendarsUsageDescription</key>
    <string>needs access to the calendar</string>
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>needs access to bluetooth</string>
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>XSAppIconAssets</key>
    <string>Resources/Media.xcassets/AppIcons.appiconset</string>
    <key>XSLaunchImageAssets</key>
    <string>Resources/Media.xcassets/LaunchImages.launchimage</string>
</dict>
</plist>

下面是我的xmlstarlet命令:

代码语言:javascript
复制
xmlstarlet sel -t -m "/plist/dict[key='CFBundleVersion']" -v string ./Info.plist

下面是输出:

代码语言:javascript
复制
Attempt to load network entity http://www.apple.com/DTDs/PropertyList-1.0.dtd
9.3
My iOS App
local.nowhere.MyiOSApp
3.3.0
321
needs access to the camera
needs access to photos
needs access to the calendar
needs access to bluetooth
UIStatusBarStyleLightContent
Resources/Media.xcassets/AppIcons.appiconset
Resources/Media.xcassets/LaunchImages.launchimage

你知道为什么它会在那个级别返回所有的'string‘节点吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-23 01:30:02

您正在匹配dict并获取string的值,所以它返回dict中的所有字符串。

检查是否存在值为CFBundleVersionkey元素并不做任何事情,如果是,则只选择dict

尝试选择key本身,然后获取第一个后续同级string的值……

代码语言:javascript
复制
xmlstarlet sel --net -t -m "/plist/dict/key[.='CFBundleVersion']" -v "following-sibling::string[1]" ./Info.plist

这将返回:

代码语言:javascript
复制
321
票数 2
EN

Stack Overflow用户

发布于 2018-02-06 07:29:44

或者为了更快地运行上面的版本(因为我不能对它发表评论),不要查找DTD并忽略STDERR:

代码语言:javascript
复制
xmlstarlet sel -t -m "/plist/dict/key[text()='CFBundleVersion']" -v "following-sibling::string[1]" ./Info.plist 2>/dev/null
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48386560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档