Question

The following sample Plist file is used for my question below.

<?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>firstDictionary</key>
    <dict>
        <key>string</key>
        <string>someText</string>
        <key>anArray</key>
        <array>
            <string>first</string>
            <string>second</string>
        </array>
    </dict>
    <key>secondDictionary</key>
    <dict>
        <key>subDictionary</key>
        <dict>
            <key>aBoolValue</key>
            <false/>
        </dict>
    </dict>
</dict>
</plist>

So my question is, since this is a Plist (working with XCode), all keys have the element name <key> and the values can be , , etc... The Key-value pair are always side-by-side (direct siblings)..

Is there a way using XQuery produce the value for a key? Like say retuenValueForKey(secondDictionary) to produce the following?

<dict>
    <key>subDictionary</key>
    <dict>
        <key>aBoolValue</key>
        <false/>
    </dict>
</dict>

My main reference so far is this link from W3Schools, but I could not get it working correctly.

Was it helpful?

Solution

You can do it in pure XPath, but if you want to wrap it in a function that should work fine too:

declare function local:returnValueForKey(
  $key as xs:string,
  $plist as element(plist)
) as element(dict)?
{
   $plist//key[. = $key]/following-sibling::*[1]/self::dict
};

local:returnValueForKey('secondDictionary', <plist>...</plist>)
=>
<dict>
  <key>subDictionary</key>
  <dict>
    <key>aBoolValue</key>
    <false/>
  </dict>
</dict>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top