XML syntax
The current version of OceanBase Database supports the XML 1.0 syntax, but does not support XML schemas or Document Type Definitions (DTDs). An XML document is mainly composed of two parts: the XML declaration and document content. The document content usually contains elements, attributes, comments, escape characters, and processing instructions.
An XML declaration has a fixed structure, such as <?xml version="1.0"?>. The XML document content has only one root element, which can contain any number of child elements. An element can contain attributes. For example, <book isbn="123-456-789">...<title>mybook</title><author>Alex</author></book> contains the isbn="123-456-789" attribute and two child elements: <title> and <author>. Child elements must be nested correctly. An empty element, such as <tag></tag>, can be represented by an empty-element tag, such as <tag/>.
An XML document uses identifiers, such as brackets (<>) and quotation marks (' or "). If the document content contains such special characters, you need to use &???; to escape them. For example, Java<tm> as the content of the child element name is escaped to <name>Java<tm></name>. The following table lists the built-in escape characters in XML 1.0.
| Before escaping | After escaping |
|---|---|
| < | < |
| > | > |
| & | & |
| " | " |
| ' | ' |
In an XML document, you can use the following syntax to define namespaces to avoid naming conflicts:
xmlns:namespace-prefix="namespaceURI"
The following sample XML document contains two table elements. Since the elements have different definitions, they must be distinguished by using namespaces. "http://www.example1.org/TR/html4/" and "http://www.example2.com.cn/furniture" are URLs that are used only as the unique names of the namespaces.
<doc>
<h:table xmlns:h="http://www.example1.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.example2.com.cn/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</doc>
Note that when a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with this namespace.
XPath
XML Path Language (XPath) is a language used to locate specific elements in an XML document. You can use XPath in EXTRACT, UPDATEXML, and other XML functions.
An XML document itself can be viewed as a node tree that contains element nodes, attribute nodes, and text nodes. XPath allows you to locate nodes in the data structure tree. XPath query results can be node sets (unordered collections without duplicate nodes), Boolean values (True or False), numbers, or strings.
The following sample code shows the complete XPath syntax. Different location nodes in an XPath is separated by /. You can omit / for the first node.
/axis::nodetest()
Here is an example:
/descendant::a, /child::text()
Parameters in the preceding example are described as follows:
descendant-or-self::paraselects theparaelements of the current node and all its descendants.self::paraselects theparaelement of the current node. If the current node does not have theparaelement,NULLis returned.child::*/child::paraselects allparagrandchildren of the current node./selects the root node.
Location steps
XPath uses location steps to select nodes in an XML document.
The following table describes the location steps supported by OceanBase Database.
| Abbreviated syntax for location step | Description |
|---|---|
| nodename | Selects all children of only this node. |
| / | Selects from the root node. |
| // | Selects nodes in the document from the current node that match the selection no matter where they are. |
| . | Selects the current node. |
| .. | Selects the parent of the current node. |
| @ | Selects attributes. |
Axes
An axis tells the XPath processor which direction to head from the current node.
The following table describes the axes supported by OceanBase Database. Among them, child is the default axis.
| Axis | Description |
|---|---|
| ancestor | Selects all ancestors (parent, grandparent, and so on) of the current node. |
| ancestor-or-self | Selects all ancestors (parent, grandparent, and so on) of the current node and the current node itself. |
| attribute | Selects all attributes of the current node. |
| child | Selects all element children of the current node. |
| descendant | Selects all descendants (children, grandchildren, and so on) of the current node. |
| descendant-or-self | Selects all descendants (children, grandchildren, and so on) of the current node and the current node itself. |
| namespace | Selects all namespace nodes of the current node. |
| parent | Selects the parent of the current node. |
| self | Selects the current node. |
Note
The current version of OceanBase Database does not support the following-sibling, following, preceding, or preceding-sibling axis.
The following table provides examples of axes.
| Example | Description |
|---|---|
| child::book | Selects all book nodes that are children of the current node. |
| attribute::lang | Selects the lang attribute of the current node. |
| child::* | Selects all element children of the current node. |
| attribute::* | Selects all attributes of the current node. |
| child::text() | Selects all text node children of the current node. |
| child::node() | Selects all children of the current node. |
| descendant::book | Selects all book descendants of the current node. |
| ancestor::book | Selects all book ancestors of the current node. |
| ancestor-or-self::book | Selects all book ancestors of the current node and itself if it is a book node. |
| child::*/child::price | Selects all price grandchildren of the current node. |
Node tests
A node test is used together with an axis to tell the XPath processor which nodes need to be selected. By default, element nodes are selected.
The following table describes the node tests supported by OceanBase Database.
| Node test | Description |
|---|---|
| para_name | Selects the node with the specified name. If the axis is attribute, this node test specifies an attribute name. If the axis is namespace, this node test specifies a namespace name. For other axes, this node test specifies an element name. |
| text() | Selects text nodes (without specifying the name). |
| node() | Selects all types of nodes (without specifying the name). |
| comment() | Selects comment nodes (without specifying the name). |
| processing-instruction(literal) | Selects pi nodes. When literal is not specified, the node test matches all pi nodes. Otherwise, it matches only the pi node of the specified name. |
Predicates
A predicate tells the XPath processor to select a specific node or a node that contains a specific value.
The following table provides some examples of predicates supported by OceanBase Database.
| Path expression | Description |
|---|---|
| //title[@lang='eng'] | Selects all title elements whose lang attribute is eng. |
| /bookstore/book[price>35.00] | Selects all book elements whose price is greater than 35.00 in the bookstore element. |
| /bookstore/book[price>35.00]/title | Select all title elements of the book element whose price is greater than 35.00 in the bookstore element. |
Wildcards
An XPath wildcard allows you to select any XML element.
The following table describes the wildcards supported by OceanBase Database.
| Wildcard | Description |
|---|---|
| * | Matches any element node. |
| @* | Matches any attribute node. |
| node() | Matches any type of node. |
| text() | Matches any text node. |
The following table provides examples of wildcards.
| Path expression | Description |
|---|---|
| /bookstore/* | Selects all element children of the bookstore element. |
| //* | Selects all elements in the document. |
| //title[@* = "attribute"] | Selects all title elements with attributes. |
Operators
The following table describes the operators supported for XPaths in OceanBase Database.
| Operator | Description | Example | Return value |
|---|---|---|---|
| = | Equal | price=9.80 | If price is 9.80, true is returned. Otherwise, false is returned. |
| != | Not equal | price!=9.80 | If price is not 9.80, true is returned. Otherwise, false is returned. |
| < | Less than | price<9.80 | If price is less than 9.80, true is returned. Otherwise, false is returned. |
| <= | Less than or equal to | price<=9.80 | If price is less than or equal to 9.80, true is returned. Otherwise, false is returned. |
| > | Greater than | price>9.80 | If price is greater than 9.80, true is returned. Otherwise, false is returned. |
| >= | Greater than or equal to | price>=9.80 | If price is greater than or equal to 9.80, true is returned. Otherwise, false is returned. |
| or | Or | price=9.80 or price=9.70 | If price is 9.80 or 9.70, true is returned. Otherwise, false is returned. |
| and | And | price>9.00 and price<9.90 | If price is greater than 9.00 and less than 9.90, true is returned. Otherwise, false is returned. |
Function expressions
The following table describes the function expressions supported for XPath definitions in the current version of OceanBase Database.
| Category | Function expression | Description |
|---|---|---|
| Boolean | boolean true() | Returns TRUE. |
| boolean false() | Returns FALSE. |