XML Built-ins ¶
XML Built-ins enable processing XML strings. Their two main functionalities involve decoding XML into a map and evaluating XPath to string.
Decode XML ¶
The XML decoding function takes an XML string and outputs a map.
Syntax
1 | Xml.decode(<xmlContent String>) |
Parameter
- xmlContent - The XML to be decoded
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | stepdef "test xml" carxml := '<car color="red" horsepower="75" isRegistered="true"> <options> <ac>cool</ac> <hifi volume="loud">sony</hifi> </options> <wheels> <wheel type="normal">front left</wheel> <wheel type="normal">front right</wheel> <wheel type="spikes">rear left</wheel> <wheel type="spikes">rear right</wheel> </wheels> </car>' decoded := Xml.decode(carxml) println(decoded + "") end |
This is decoded into:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { car={ @isRegistered=true, @horsepower=75, @color=red, options={ ac=cool, hifi={#text=sony, @volume=loud} }, wheels={ wheel=[{#text=front left, @type=normal}, {#text=front right, @type=normal}, {#text=rear left, @type=spikes}, {#text=rear right, @type=spikes}] } } } |
Evaluate XPath ¶
The evaluateXpath
method selects an XPath Node's attribute. If the XPath resolves to multiple nodes, the first Node is returned.
Syntax
1 | Xml.evaluateXpath(<xpathExpression String>, <xmlString Variable>) |
Parameters
-
xpathExpression - The XPath expression being evaluated
-
xmlString - The variable that holds the XML string
Single Node Example
1 | Xml.evaluateXpath("//hifi/@volume", carxml) |
When applied to:
1 2 3 4 | <options> <ac>cool</ac> <hifi volume="loud">sony</hifi> </options> |
This returns:
1 | "loud"
|
Multiple Nodes Example
1 | Xml.evaluateXpath("//wheel", carxml) |
When applied to:
1 2 3 4 5 6 | <wheels> <wheel type="normal">front left</wheel> <wheel type="normal">front right</wheel> <wheel type="spikes">rear left</wheel> <wheel type="spikes">rear right</wheel> </wheels> |
This returns:
1 | { wheel={ @type=normal } } |
Evaluate XPath to List ¶
The evaluateXpathToList
method retrieves a list of decoded objects with the same node name by specifying its XPath.
Syntax
1 | Xml.evaluateXpathToList(<xpathExpression String>, <xmlString Variable>) |
Parameters
-
xpathExpression - The XPath expression being evaluated
-
xmlString - The variable that holds the XML string
Example
1 | Xml.evaluateXpathToList("//wheel", carxml) |
When applied to:
1 2 3 4 5 6 | <wheels> <wheel type="normal">front left</wheel> <wheel type="normal">front right</wheel> <wheel type="spikes">rear left</wheel> <wheel type="spikes">rear right</wheel> </wheels> |
This returns:
1 2 3 4 5 6 | [ { wheel={@type=normal} }, { wheel={@type=normal} }, { wheel={@type=spikes} }, { wheel={@type=spikes} } ] |
Evaluate XPath to List with Attribute Names¶
By specifying an attribute name from the XPath expression, evaluateXpathToList
returns a list of all attributes that match it.
Example 1 - Matches All Attributes with Name "Type" That Are below the Document Root Node
1 | Xml.evaluateXpathToList("//@type", carxml) |
Example 2 - Matches All Text Nodes for Every Element "Wheel"
1 | Xml.evaluateXpathToList("//wheel/#text", carxml) |
When applied to:
1 2 3 4 5 6 | <wheels> <wheel type="normal">front left</wheel> <wheel type="normal">front right</wheel> <wheel type="spikes">rear left</wheel> <wheel type="spikes">rear right</wheel> </wheels> |
1 2 3 4 | // [normal, normal, spikes, spikes] // [front left, front right, rear left, rear right] |
Evaluate XPath to String ¶
The evaluateXPathToString
method returns a string containing the attribute parameter belonging to a XPath expression.
Syntax
1 | Xml.evaluateXpathToList(<xpathExpression String>, <xmlString Variable>) |
Parameters
-
xpathExpression - The XPath expression being evaluated to a string
-
xmlString - The variable that holds the XML string
Example
1 | Xml.evaluateXpathToString("//hifi", carxml) |
1 2 3 4 | <options> <ac>cool</ac> <hifi volume="loud">sony</hifi> </options> |
This returns:
1 | sony
|
Use Case: Reading a Node's Value¶
Specifying an XPath element's local name using the evaluateXpathToString
method returns its content as a result.
Example
1 | Xml.evaluateXpathToString("//*[local-name()=‘identifier']") |
When applied to:
1 2 3 4 5 6 7 8 9 10 | <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns22:createAccountResponse xmlns:ns22="http://qitasc.com/xsd/Accounts-v2.0"> <ns22:return> <ns16:identifier xmlns:ns16="http://qitasc.com/xsd/2011/01">12345</ns16:identifier> <ns16:parent xmlns:ns16="http://qitasc.com/xsd/2011/01"/> </ns22:return> </ns22:createAccountResponse> </soapenv:Body> </soapenv:Envelope> |
This returns:
1 | 12345
|
Evaluate XPath to Number ¶
The evaluateXpathToNumber
method retrieves a BigDecimal value from the XML object.
Syntax
1 | Xml.evaluateXpathToList(<xpathExpression String>, <xmlString Variable>) |
Parameters
-
xpathExpression - The XPath expression being evaluated to a number
-
xmlString - The variable that holds the XML string
Example
1 | Xml.evaluateXpathToNumber("/car/@horsepower", carxml) |
When applied to:
1 | <car color="red" horsepower="75" isRegistered="true"> |
This returns:
1 | 75.0
|
Evaluate XPath to Boolean ¶
The evaluateXpathToBoolean
method retrieves Boolean values from the XML object.
Syntax
1 | Xml.evaluateXpathToList(<xpathExpression String>, <xmlString Variable>) |
Parameters
-
xpathExpression - The XPath expression being evaluated to a number
-
xmlString - The variable that holds the XML string
Example
1 | Xml.evaluateXpathToBoolean("/car/@isRegistered", carxml) |
When applied to:
1 | <car color="red" horsepower="75" isRegistered="true"> |
This returns:
1 | true
|
Pretty Print ¶
The prettyPrint
method applies Prettyprint formatting conventions to XML to improve its readability.
Syntax
1 | Xml.prettyPrint(<xml String>, <indentation Number>, <charset String>) |
Parameters
-
xml - The XML object
-
indentation (Optional) - The indentation level in spaces
- Default is set to
4
- Default is set to
-
charset - (Optional) The charset
- Default is set to
UTF-8
​ Example 1 - Without Specified Indentation Level or Charset1
Xml.prettyPrint("<car><wheels><wheel>1</wheel><wheel>2</wheel></wheels></car>")
- Default is set to
Example 2 - With Indentation Level and Charset Specified
1 | Xml.prettyPrint("<car><wheels><wheel>1</wheel><wheel>2</wheel></wheels></car>", 4, "UTF-8") |
Both above examples will result in the following:
1 2 3 4 5 6 | <car> <wheels> <wheel>1</wheel> <wheel>2</wheel> </wheels> </car> |
XML Ticket Cache Configuration ¶
This configures the cache for XML Tickets used in Verification.
Syntax
1 2 3 4 5 | XMLTicketCache { useCache = <Boolean> maxCachedPropertiesNumberForTicket = <Number> cachedPropertyExpirationTimeoutInMinutes = <Number> } |
Parameters
-
useCache -
true
(Default) if cache strategy should be used for XML Ticket propertiesfalse
otherwise
-
maxCachedPropertiesNumberForTicket - The maximum number of properties that can be stored in cache per ticket
-
cachedPropertyExpirationTimeoutInMinutes - The number of minutes a property will remain in cache after last access
Example
1 2 3 4 5 | XMLTicketCache { useCache = true maxCachedPropertiesNumberForTicket = 8 cachedPropertyExpirationTimeoutInMinutes = 15 } |