XML syntax
OceanBase Database supports Extensible Markup Language (XML) 1.0 syntax but does not support XMLSchema or DTD. An XML document consists of a statement declaration and document content. The document content typically includes elements, attributes, comments, escape characters, and processing instructions.
XML statement declarations have a fixed structure, such as <?xml version="1.0"?>. An XML document has only one root element, which can contain any number of child elements. Elements can have attributes. For example, the <book isbn="123-456-789">...<title>mybook</title><author>Alex</author></book> element contains an attribute isbn="123-456-789" and two child elements, <title> and <author>. Child elements must be properly nested. If an element is empty, such as <tag></tag>, you can use the abbreviation <tag/>.
If special characters such as <> and quotation marks are used in the document content, you must use &???; to escape them. For example, the name element with the content Java<tm> is escaped as <name>Java<tm></name>. The following table lists the built-in escape characters in XML 1.0.
| Escape character | Escape symbol |
|---|---|
| < | < |
| > | > |
| & | & |
| " | " |
| ' | ' |
You can define a namespace in an XML document to avoid naming conflicts. The syntax for defining a namespace is as follows:
xmlns:namespace-prefix="namespaceURI"
The following example defines two tables. You must use namespace to distinguish them. "http://www.example1.org/TR/html4/" and "http://www.example2.com.cn/furniture" are the addresses that identify the namespaces. These addresses assign unique names to 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 the same namespace.
XPath
XPath (XML Path Language) is a language for locating specific elements in an XML document. It can be used in expressions such as EXTRACT and UPDATEXML.
An XML document can be viewed as a node tree, including element nodes, attribute nodes, and text nodes. XPath provides the capability to find nodes within a data structure tree. The query results can be a node set (an unordered collection of unique nodes), a boolean value (True or False), a number, or a string.
The complete syntax of XPath is as follows. Each location node is separated by a /. The first node can omit the /.
/axis::nodetest()
The following example provides a detailed explanation of XPath syntax.
/descendant::a,/child::text()
In the example above:
descendant-or-self::para: Selects theparaelements of the current node and all its descendants.self::para: Selects theparaelement of the current node. If it does not exist, it returnsNULL.child::*/child::para: First selects all child elements of the current node and then finds theparaelements of those child elements./: 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.
| Expression | Description |
|---|---|
| nodename | Selects all child nodes of this node. |
| / | Selects the root node. |
| // | Selects all nodes in the document that match the selection, regardless of their position. |
| . | Selects the current node. |
| .. | Selects the parent node of the current node. |
| @ | Selects attributes. |
Axes
Axes specify the direction of XPath queries, with the current node as the origin.
The following table describes the axes supported by OceanBase Database. child is the default axis.
| Axis | Description |
|---|---|
| ancestor | Selects all ancestors of the current node (such as parent and grandparent nodes). |
| ancestor-or-self | Selects all ancestors of the current node (such as parent and grandparent nodes) and the current node itself. |
| attribute | Selects all attributes of the current node. |
| child | Selects all child elements of the current node. |
| descendant | Selects all descendant elements of the current node (such as child and grandchild nodes). |
| descendant-or-self | Selects all descendant elements of the current node (such as child and grandchild nodes) and the current node itself. |
| namespace | Selects all namespace nodes of the current node. |
| parent | Selects the parent node of the current node. |
| self | Selects the current node. |
Note
OceanBase Database does not support the following-sibling, following, preceding, and preceding-sibling axes in the current version.
The following table describes examples of axes.
| Example | Description |
|---|---|
| child::book | Selects all book nodes that are child elements of the current node. |
| attribute::lang | Selects the lang attribute of the current node. |
| child::* | Selects all child elements of the current node. |
| attribute::* | Selects all attributes of the current node. |
| child::text() | Selects all text child nodes of the current node. |
| child::node() | Selects all child nodes of the current node. |
| descendant::book | Selects all book nodes that are descendants of the current node. |
| ancestor::book | Selects all book nodes that are ancestors of the current node. |
| ancestor-or-self::book | Selects all book nodes that are ancestors of the current node and the current node itself (if the current node is a book node). |
| child::*/child::price | Selects all price nodes that are grandchildren of the current node. |
Node tests
Node tests specify the content to be queried in XPath, based on the axis information. By default, the value is element.
The following table describes the node tests supported by OceanBase Database.
| Node test | Description |
|---|---|
| para_name | Selects a node with the specified attribute name. If the preceding axis is attribute, the value is the attribute name. If the preceding axis is ns, the value is the ns name. Otherwise, the value is the element name. |
| text() | Selects a text node. The name cannot be specified. |
| node() | Selects all types of nodes. The name cannot be specified. |
| comment() | Selects a comment node. The name cannot be specified. |
| processing-instruction(literal) | Selects a pi node. If literal is empty, it matches any pi node. Otherwise, it matches only the pi node with the specified value. |
Predicates
Predicates are used to select a specific node or a node with a specified value.
The following table describes the predicates supported by OceanBase Database.
| Path expression | Result |
|---|---|
| //title[@lang='eng'] | Selects all title elements that have the lang attribute with the value eng. |
| /bookstore/book[price>35.00] | Selects all book elements of the bookstore element, where the price element has a value greater than 35.00. |
| /bookstore/book[price>35.00]/title | Selects all title elements of the book elements of the bookstore element, where the price element has a value greater than 35.00. |
Wildcards
Wildcards in XPath are used to select any XML elements.
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 describes examples of wildcards.
| Path expression | Description |
|---|---|
| /bookstore/* | Selects all child elements of the bookstore element. |
| //* | Selects all elements in the document. |
| //title[@* = "attribute"] | Selects all title elements that have attributes. |
Operators
The following table describes the operators supported by OceanBase Database in XPath expressions.
| Operator | Description | Example | Return value |
|---|---|---|---|
| = | Equal to | price=9.80 | Returns TRUE if the value of the price element is 9.80. Otherwise, it returns FALSE. |
| != | Not equal to | price!=9.80 | Returns TRUE if the value of the price element is not 9.80. Otherwise, it returns FALSE. |
| < | Less than | price<9.80 | Returns TRUE if the value of the price element is less than 9.80. Otherwise, it returns FALSE. |
| <= | Less than or equal to | price<=9.80 | Returns TRUE if the value of the price element is less than or equal to 9.80. Otherwise, it returns FALSE. |
| > | Greater than | price>9.80 | Returns TRUE if the value of the price element is greater than 9.80. Otherwise, it returns FALSE. |
| >= | Greater than or equal to | price>=9.80 | Returns TRUE if the value of the price element is greater than or equal to 9.80. Otherwise, it returns FALSE. |
| or | Logical OR | price=9.80 or price=9.70 | Returns TRUE if the value of the price element is 9.80 or 9.70. Otherwise, it returns FALSE. |
| and | Logical AND | price>9.00 and price<9.90 | Returns TRUE if the value of the price element is greater than 9.00 and less than 9.90. Otherwise, it returns FALSE. |
Function expressions
The following table describes the function expressions supported by OceanBase Database in XPath.
| Category | Function | Description |
|---|---|---|
| Boolean function | boolean true() | Returns TRUE. |
| boolean false() | Returns FALSE. |
