Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

XML.Query

Performs an XPath query.

Component Version macOS Windows Linux Server iOS SDK
XML 3.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "XML.Query"; xml; path { ; namespaceList; Flags } )   More

Parameters

Parameter Description Example Flags
xml The XML you'd like to parse.
Can be XML as text or the reference returned by XML.Parse function, so you can make several times queries to the XML without parsing it each time.
"<hello>Hello World</hello>"
path The XPath you'd like to query. "/*"
namespaceList List of namespaces.
This is a list of known namespaces in "<prefix1>=<href1> <prefix2>=<href2> ..." format.
Version 11..6 and newer will remove quotes surrounding the URLs.
"SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/" Optional
Flags Various Flags.
Add 1 to ignore errors in xml and continue parsing. This may lead to not everything in the xml being read.
Add 2 to return a list of xml nodes as text instead of xml. (new in 5.4)
Add 4 if you do not like the XML to be formatted.
Add 8 to remove all namespaces before query to have queries easier. (new in 6.3)
Add 16 to return only first item of a list in result. (new in 11.3)
Add 32 to return a list of XML entries without a wrapping <result> entry. (new in 11.3)
0 Optional

Result

Returns xml text.

Description

Performs an XPath query.
Result is either simple text, a xml node or several xml nodes in a result root node. XML is returned formatted.
Please check on the web for XPath manuals and tutorials.
Version 3.4 of the plugins now also return string, number and boolean results instead of just XML nodes.

If the query to find the node is too difficult, you can of course use text functions instead. For example use Text.FindBetween and Text.DecodeFromXML.

See XML Path Language (XPath) specification here:
https://www.w3.org/TR/1999/REC-xpath-19991116/

Examples

Query the Hello World text:

MBS( "XML.Query"; "<hello>Hello World</hello>"; "/hello/text()" )

Example result: Hello World

Query an attribute of a XML Node:

MBS("XML.Query"; "<OrderRequestHeader orderID=\"1629\" orderDate=\"2013-07-26T12:20:27\" type=\"new\"><Money>123.00</Money></OrderRequestHeader>"; "/OrderRequestHeader/@orderID" )

Example result:
<?xml version="1.0" encoding="UTF-8"?>
orderID="1629"

Query an attribute of a XML Node as string:

MBS("XML.Query"; "<OrderRequestHeader orderID=\"1629\" orderDate=\"2013-07-26T12:20:27\" type=\"new\"><Money>123.00</Money></OrderRequestHeader>"; "string(/OrderRequestHeader/@orderID)" )

Example result: 1629

Query all text from all nodes named reference ignoring namespaces:

MBS( "XML.Query"; XML Query::XML; "//*[local-name()='reference']/text()"; ""; 2 )

Finds a Field node where attribute name is "Class":

MBS( "XML.Query"; $xml; "/CDETS/Defect/Field[@name='Class']/text()")

Finds second companyname node in xml data with namespace invoice:

MBS("XML.Query"; XML Query::XML; "//invoice:companyname[2]"; "invoice=http://www.forum-datenaustausch.ch/invoice"; 2)

Query node without namespaces:

MBS( "XML.Query"; "<cfdi:Invoice invoiceTotal=\"450.24\"> </cfdi:Invoice>" ; "/Invoice/@invoiceTotal"; ""; 8+2)

Example result: 450.24

Query attributes:

MBS( "XML.Query";
"<a><b Guid=\"123\">Entry1</b><b Guid=\"456\">Entry2</b></a>";
"/a/b/@Guid"; ""; 2 )

Example result:
123
456

Try result as list:

MBS( "XML.Query"; "<test><hello>Hello World1</hello><hello>Hello World2</hello><hello>Hello World3</hello></test>"; "//test/hello"; ""; 32 )

Example result:
<hello>Hello World1</hello>
<hello>Hello World2</hello>
<hello>Hello World3</hello>

Play with indexes picking the node and picking from the result list:

# we can find all emails in all nodes and get text
Set Variable [$allEmails; Value: MBS( "XML.Query"; $xml; "//invoice:email"; 2 ) ]

# go through records and query first email of each record.
Set Variable [$firstEmailOfEach; Value: MBS( "XML.Query"; $xml; "//invoice:email[1]"; 2 ) ]

# pick first email of each record and return us first one found:
Set Variable [$first email of allEmails; Value: MBS( "XML.Query"; $xml; "(//invoice:email[1])[1]"; 2 ) ]
# pick first email of each record and return us second one found:
Set Variable [$second email of allEmails; Value: MBS( "XML.Query"; $xml; "(//invoice:email[1])[2]"; 2 ) ]
# pick first email of each record and return us third one found:
Set Variable [$third email of allEmails; Value: MBS( "XML.Query"; $xml; "(//invoice:email[1])[3]"; 2 ) ]

See also

Release notes

  • Version 12.1
    • Fixed a problem with XML.Query, where the first only flag was not handled correctly.
  • Version 12.0
    • Changed XML.Query to remove surrounding quotes for namespaces.
  • Version 11.3
    • Added flag 16 for XML.Query to only return first item of result.
    • Added flag 32 for XML.Query to return multiple XML nodes as a list with each as XML.
  • Version 8.0
    • Changed XML.Query to return attribute list properly.

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 6th February 2023


XML.Parse - XML.ReadContainer