Product Documentation

XPath and HTML, XML, or JSON Expressions

Mar 20, 2012

The default syntax expression engine supports expressions for evaluating and retrieving data from HTML, XML, and JavaScript Object Notation (JSON) files. This enables you to find specific nodes in an HTML, XML, or JSON document, determine if a node exists in the file, locate nodes in XML contexts (for example, nodes that have specific parents or a specific attribute with a given value), and return the contents of such nodes. Additionally, you can use XPath expressions in rewrite expressions.

The default syntax expression implementation for XPath comprises a default syntax expression prefix (such as “HTTP.REQ.BODY”) that designates HTML or XML text, and the XPATH operator that takes the XPath expression as its argument.

HTML files are a largely free-form collection of tags and text elements. You can use the XPATH_HTML operator, which takes an XPath expression as its argument, to process HTML files. JSON files are either a collection of name/value pairs or an ordered list of values. You can use the XPATH_JSON operator, which takes an XPath expression as its argument, to process JSON files.

Table 1. XPath and JSON Expression Prefixes That Return Text

XPath Prefix

Description

<text>.XPATH(xpathex)

Operate on an XML file and return a Boolean value.

For example, the following expression returns a Boolean TRUE if a node called “creator” exists under the node “Book” within the first 1000 bytes of the XML file.

HTTP.REQ.BODY(1000).XPATH(xp%boolean(//Book/creator)%)

Parameters:

xpathex - XPath Boolean expression

<text>.XPATH(xpathex)

Operate on an XML file and return a value of data type “double.”

For example, the following expression converts the string “36” (a price value) to a value of data type “double” if the string is in the first 1000 bytes of the XML file:

HTTP.REQ.BODY(1000).XPATH(xp%number(/Book/price)%)

Parameters:

xpathex - XPath numeric expression

<text>.XPATH(xpathex)

Operate on an XML file and return a node-set or a string. Node-sets are converted to corresponding strings by using the standard XPath string conversion routine.

For example, the following expression selects all the nodes that are enclosed by “/Book/creator” (a node-set) in the first 1000 bytes of the body:

HTTP.REQ.BODY(1000).XPATH(xp%/Book/creator%)

Parameters:

xpathex - XPath expression

<text>.XPATH_HTML(xpathex)

Operate on an HTML file and return a text value.

For example, the following expression operates on an HTML file and returns the text enclosed in <title></title> tags if the title HTML element is found in the first 1000 bytes:

HTTP.REQ.BODY(1000).XPATH_HTML(xp%/html/head/title%)

Parameters:

xpathex - XPath text expression

<text>.XPATH_HTML_WITH_MARKUP(xpathex)

Operate on an HTML file and return a string that contains the entire selected portion of the document, including markup such as including the enclosing element tags.

The following expression operates on the HTML file and selects all content within the <title> tag, including markup.

HTTP.REQ.BODY(1000).XPATH_HTML_WITH_MARKUP( xp%/html/head/title%)

The portion of the HTML body that is selected by the expression is marked for further processing.

Parameters:

xpathex - XPath expression

<text>.XPATH_JSON(xpathex)

Operate on a JSON file and return a Boolean value.

For example, consider the following JSON file:

{ "Book":{ "creator":{ "person":{ "name":'<name>' } }, "title":'<title>' } }

The following expression operates on the JSON file and returns a Boolean TRUE if the JSON file contains a node named “creator,” whose parent node is “Book,” in the first 1000 bytes:

HTTP.REQ.BODY(1000).XPATH_JSON(xp%boolean(/Book/creator)%)

Parameters:

xpathex - XPath Boolean expression

<text>.XPATH_JSON(xpathex)

Operate on a JSON file and return a value of data type “double.”

For example, consider the following JSON file:

{ "Book":{ "creator":{ "person":{ "name":'<name>' } }, "title":'<title>', "price":"36" } }

The following expression operates on the JSON file and converts the string “36” to a value of data type “double” if the string is present in the first 1000 bytes of the JSON file.

HTTP.REQ.BODY(1000).XPATH_JSON(xp%number(/Book/price)%)

Parameters:

xpathex - XPath numeric expression

<text>.XPATH_JSON(xpathex)

Operate on a JSON file and return a node-set or a string. Node-sets are converted to corresponding strings by using the standard XPath string conversion routine.

For example, consider the following JSON file:

{ "Book":{ "creator":{ "person":{ "name":'<name>' } }, "title":'<title>' } }

The following expression selects all the nodes that are enclosed by “/Book" (a node-set) in the first 1000 bytes of the body of the JSON file and returns the corresponding string value, which is "<name><title>":

HTTP.REQ.BODY(1000).XPATH_JSON(xp%/Book%)

Parameters:

xpathex - XPath expression

<text>.XPATH_JSON_WITH_MARKUP(xpathex)

Operate on an XML file and return a string that contains the entire portion of the document for the result node, including markup such as including the enclosing element tags.

For example, consider the following JSON file:

{"Book":{ "creator":{ "person":{ "name":'<name>' } }, "title":'<title>' } }

The following expression operates on the JSON file and selects all the nodes that are enclosed by “/Book/creator" in the first 1000 bytes of the body, which is “creator:{ person:{ name:'<name>' } }.”

HTTP.REQ.BODY(1000).XPATH_JSON_WITH_MARKUP(xp%/Book/creator%)

The portion of the JSON body that is selected by the expression is marked for further processing.

Parameters:

xpathex - XPath expression

<text>.XPATH_WITH_MARKUP(xpathex)

Operate on an XML file and return a string that contains the entire portion of the document for the result node, including markup such as including the enclosing element tags.

For example, the following expression operates on an XML file and selects all the nodes enclosed by “/Book/creator" in the first 1000 bytes of the body.

HTTP.REQ.BODY(1000).XPATH_WITH_MARKUP(xp%/Book/creator%)

The portion of the JSON body that is selected by the expression is marked for further processing.

Parameters:

xpathex - XPath expression