|
@xpath() Function |
|
|
@xpath() function is used to query an XML string or a pre-parsed Node object. (since v5.0) Syntax: @xpath(xml-input, xpath-expression [,encoding]) xml-input It can either be a plain XML string or pre-parsed Node object to query. xpath-expression The XPath expression that queries the XML Input. Note that the XPath query should return a string result NOT a NodeList. encoding Default 'UTF-8' Examples: @parsexml('<root><item><PRICE>1200</PRICE></item></root>',v:NODE) @xpath(v:NODE, '//PRICE') @// result: 1200 @xpath(<root><item><PRICE curr="USD">1200</PRICE></item></root>, '//PRICE/@curr') @// result: USD
Prefix usage in an XPath expression:
v:XML <cns:customer id="1" xmlns:cns="http://www.localhost.com/customer"> <ons:order xmlns:ons="http://www.localhost.com/order"> <date>01-01-2011</date> <product> <name>Notebook</name> <price>1000</price> <quantity>1</quantity> </product> <product> <name>Notebook</name> <price>1000</price> <quantity>1</quantity> </product> </ons:order> </cns:customer> <cns2:customer id="2" xmlns:cns2="http://www.localhost.com/customer2"> <ons:order xmlns:ons="http://www.localhost.com/order"> <date>01-01-2011</date> <product> <name>Desktop Computer</name> <price>750</price> <quantity>2</quantity> </product> </ons:order> </cns2:customer> </root>
@parsexml(v:XML, v:NODE) @xpath(v:NODE, '/root/cns2:customer[@id = 2]/ons:order/product/name') @// result: Desktop Computer
@xpath(v:NODE, '/root/cns2:customer/ons:order/product/name') @// result: Notebook
An XPath expression without namespace prefix can also be used for the above example. To make it happen, @parsexml() function should be used as follows or an XML string should be used as the input. For the both of the cases the namespace prefixes will be ignored and the value of the first node that matches will be returned. @set(v:XML,'<root><cns:customer id="1" ... </cns2:customer></root>') @parsexml(v:XML, v:NODE,, false) @xpath(v:NODE, '/root/customer[@id = 2]/order/product/name') @// result: Desktop Computer
or @xpath(v:XML, '/root/customer[@id = 2]/order/product/name') @// result: Desktop Computer
Default Namespace Usage:
v:XML <cns:customer id="1" xmlns:cns="http://www.localhost.com/customer"> <order xmlns="http://www.localhost.com/order"> <date>01-01-2011</date> <product> <name>Notebook</name> <price>1000</price> <quantity>1</quantity> </product> <product> <name>Notebook</name> <price>1000</price> <quantity>1</quantity> </product> </order> </cns:customer> <cns2:customer id="2" xmlns:cns2="http://www.localhost.com/customer2"> <order xmlns="http://www.localhost.com/order"> <date>01-01-2011</date> <product> <name>Desktop Computer</name> <price>750</price> <quantity>2</quantity> </product> </order> </cns2:customer> </root>
@parsexml(v:XML, v:NODE) @xpath(v:NODE, '/root/cns2:customer[@id = 2]/:order/:product/:name') @// result: Desktop Computer
If a namespace is defined on a node and there is no namespace prefix used for it (the default namespace) ":" should be used as the prefix as in the case above.
See @parsexml() |