简体   繁体   中英

Why is XPATH (Java 7) selecting too many nodes?

I have a rather simple plist XML. It looks like this:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>cast</key>
<array>
    <dict>
        <key>name</key>
        <string>Michael Boatman</string>
    </dict>
    <dict>
        <key>name</key>
        <string>Dillon Casey</string>
    </dict>
</array>
<key>directors</key>
<array>
    <dict>
        <key>name</key>
        <string>Jace Alexander</string>
    </dict>
</array>
</dict>
</plist>

So the XML contains two cast members, and one director. My XPATH expression is like this:

"/plist/dict[key='directors']/array/dict/string/text()"

This expression fetches all three records. Why is this the case? I tried numerous more complicated expressions (like dict/key[name()='director']/../), but it always results in the same. It fetches three records when I query for "directors" or "cast", and zero records when I query for another string.

The top-level dict element contains a subelement key with value directors . Therefore it, and consequently all its three array children, are legitimately selected by your XPath expression.

You should instead select

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM