关键字字符串模式的XML XPath

时间:2018-06-29 21:04:49

标签: xml xpath xml-parsing

我有一个如下的XML文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
    <dict>
        <key>CFBundleExecutable</key>
        <string>p4v</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>2018.2</string>
        <key>CFBundleVersion</key>
        <string>2018.2/1666551</string>
        <key>CFBundleGetInfoString</key>
        <string>2018.2, Copyright 2018 Perforce Software, Inc.</string>
        <key>CFBundleIconFile</key>
        <string>application.icns</string>
        <key>P4RevString</key>
        <string>P4V/MACOSX1013X86_64/2018.2/1666551 (2018/05/30)</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>NSHighResolutionCapable</key>
        <string>True</string>
        <key>CFBundleName</key>
        <string>P4V</string>
        <key>CFBundleSignature</key>
        <string>P4VC</string>
        <key>CFBundleIdentifier</key>
        <string>com.perforce.p4v</string>
    </dict>
</plist>

我想检索键 CFBundleShortVersionString

的字符串值 2018.2

什么是XPath。

我尝试过

/plist/dict[starts-with(key,'CFBundleShortVersionString')]/string

但是没有用。

1 个答案:

答案 0 :(得分:1)

尝试此XPath:

/plist/dict/key[text()='CFBundleShortVersionString']/following-sibling::string[1]/text()

其结果为2018.2