XPath and HTML, XML, or JSON expressions
The Advanced policy infrastructure 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 Advanced policy expression implementation for XPath comprises an Advanced policy 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.
Operate on an XML file and return a Boolean value..XPATH(xpathex): 例如,下面的表达式返回一个薄熙来olean 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
Operate on an XML file and return a value of data type “double.”.XPATH(xpathex): 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
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..XPATH(xpathex): 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
.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
tags if the title HTML element is found in the first 1000 bytes:<\title> HTTP.REQ.BODY(1000).XPATH_HTML(xp%/html/head/title%)
Parameters:
xpathex - XPath text expression
.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
tag, including markup.HTTP.REQ.BODY(1000).XPATH_HTML_WITH_MARKUP( xp%/html/head/title%)
Example:
Milton Paradise Lost
The portion of the HTML body that is selected by the expression is marked for further processing.
Parameters:
xpathex - XPath expression
.XPATH_JSON(xpathex) Operate on a JSON file and return a Boolean value.
For example, consider the following JSON file:
{“Book”:{“creator”:{“person”:{“name”:’
’} }, “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
Operate on a JSON file and return a value of data type “double.”.XPATH_JSON(xpathex) For example, consider the following JSON file:
{"Book":{"creator":{"person":{"name":'\
'} }, "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
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..XPATH_JSON(xpathex) For example, consider the following JSON file:
{"Book":{"creator":{"person":{"name":'\
'}}, "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 “
”: HTTP.REQ.BODY(1000).XPATH_JSON(xp%/Book%)
Parameters:
xpathex - XPath expression
.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”:’
’ } }, “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:’
’ } }.” 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
.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