Syntax
XMLSelect <xpath> <xml> <nslist>Description
Select XML nodes using an XPath expression.Parameters
<xpath>
- The XPath expression that will be used to select the nodes returned
<xml>
- The XML document to which the XPath expression will be applied.
<nslist>
- A list of name=value pairs in which the name is the name used for the namespace in the XPath expression (i.e. the namespace identifier) and the value is the actual namespace. (Example below.)
Remarks
The most common reason why XPath queries don't return expected results is not using namespace identifiers where required.This statement is most useful when a single XML node is returned. If multiple XML nodes will be returned, which must then be individually processed further (i.e. not processed as a group), use the XMLMatch statement instead. If the text from the selected XML node is what is needed, consider using XMLSelectText.
The PhantomScript XML-handling statements are XMLMatch, XMLSelect, XMLSelectText, and XMLTransform.
Example
Local peopleData = "<People></People>"
AddPerson
XMLData=peopleData
Name="John"
Age=27
Gender="Male"
Result=peopleData
AddPerson
XMLData=peopleData
Name="Mary"
Age=24
Gender="Female"
Result=peopleData
AddPerson
XMLData=peopleData
Name="Caryl"
Age=32
Gender="Male"
Result=peopleData
! No namespace
ages = XMLSelect "//Age" peopleData ""
johnsGender = XMLSelect "//Person[@Name='John']/Gender/text()" peopleData ""
MessageBox Text=ages & "~n" & johnsGender
ExitScript
! Procedure to add a person to our little database.
Procedure AddPerson ArgList=XMLData,Name,Age,Gender
Local xml = StringBefore StringAfter XMLData "<People>" "</People>"
xml = xml & "<Person Name=""" & Name & """>"
xml = xml & "<Age>" & Age & "</Age>"
xml = xml & "<Gender>" & Gender & "</Gender>"
xml = xml & "</Person>"
SetResultValue "<People>" & xml & "</People>"
EndProcedureThe above script fragment constructs a three-element XML database containing people. XMLSelect is then used to retrieve the XML age nodes and John's gender node. The nodes are then displayed in a message box. The message box's contents are
<Age>27</Age><Age>24</Age><Age>32</Age>
Male