Table of Contents

XPath

C10 - initial

xpath (Sifts)
TypeCorrade progressive sift
SiftXPath
Parametersa string representing a fully qualified XML path
DescriptionThe XPath sift will return all items matching an XML path.
Last ChangesNone

The XPath sift will return all items matching an XML path.

Arguments

Name Type Description Example
default a string a namespace x
path a string a fully qualified path /x:ArrayOfResult/x:Result/x:Description

Example

Suppose that extracting the Description from the first Result tag of the following abbreviated XML is desired:

<ArrayOfResult
	xmlns="http://lookup.dbpedia.org/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<Result>
		<Label>Gomel</Label>
		<URI>http://dbpedia.org/resource/Gomel</URI>
		<Description>
Gomel; also Homiel, Homel is the administrative center of Gomel Voblast and the second-largest city in Belarus. It has a population of 482,652 (2009 census).
</Description>
		<Classes>
			<Class>
				<Label>populated place</Label>
[...]

Using the XPath sift the task can be accomplished by sending the following command to Corrade:

        llInstantMessage(CORRADE, 
            wasKeyValueEncode(
                [
                    "command", "HTTP",
                    "group", wasURLEscape(GROUP),
                    "password", wasURLEscape(PASSWORD),
                    "action", "get",
                    "type", "text",
                    "URL", wasURLEscape("http://lookup.dbpedia.org/api/search/PrefixSearch?QueryClass=&MaxHits=5&QueryString=Homie"),
                    "sift", wasListToCSV(
                        [
                            "XPath",
                            // Ignoring namespaces:
                            //wasURLEscape("//*[local-name()='ArrayOfResult']/*[local-name()='Result']/*[local-name()='Description']")
                            // The proper way:
                            wasURLEscape(
                                wasKeyValueEncode(
                                    [
                                        "default", "x",
                                        "path", wasURLEscape("/x:ArrayOfResult/x:Result/x:Description")
                                    ]
                                )
                            )
                        ]
                    ),
                    "callback", wasURLEscape(URL)
                ]
            )
        );

First thing to notice is that the XML to be found at the mentioned URL contains a default XML namespace xmlns="http://lookup.dbpedia.org/" with no prefix such that, all the tags from the snippet are to be found in no namespace meaning that querying them via XPath will not be possible.

The parameter that has to be passed to the XPath sift is a key-value pair consisting of a default/no namespace keyword and an XPath:

                    "sift", wasListToCSV(
                        [
                            "XPath",
                            //wasURLEscape("//*[local-name()='ArrayOfResult']/*[local-name()='Result']/*[local-name()='Description']")
                            wasURLEscape(
                                wasKeyValueEncode(
                                    [
                                        "default", "x",
                                        "path", wasURLEscape("/x:ArrayOfResult/x:Result/x:Description")
                                    ]
                                )
                            )
                        ]
                    ),

By assigning a prefix (x) to the default namespace/no namespace, the tags can then be queried using the XPath /x:ArrayOfResult/x:Result/x:Description.

For XML definitions that contain a namespace prefix, the default keyword will be ignored and the XML can be queried using the defined namespace prefix.

Note that the parameter passed to XPath is an escaped key-value pair that itself contains an escaped parameter. This is done to prevent collisions with the other key-value data that is sent to Corrade and should always be used when passing parameters for the XPath sift.