You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/functions/openxml-transact-sql.md
+42-42Lines changed: 42 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
title: "OPENXML (Transact-SQL)"
3
-
description: "OPENXML provides a rowset view over an XML document."
3
+
description: OPENXML provides a rowset view over an XML document.
4
4
author: MikeRayMSFT
5
5
ms.author: mikeray
6
6
ms.reviewer: randolphwest
7
-
ms.date: 05/04/2023
7
+
ms.date: 06/20/2024
8
8
ms.service: sql
9
9
ms.subservice: t-sql
10
10
ms.topic: reference
@@ -22,7 +22,7 @@ dev_langs:
22
22
23
23
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
24
24
25
-
OPENXML provides a rowset view over an XML document. Because OPENXML is a rowset provider, OPENXML can be used in [!INCLUDE[tsql](../../includes/tsql-md.md)] statements in which rowset providers such as a table, view, or the OPENROWSET function can appear.
25
+
`OPENXML` provides a rowset view over an XML document. Because `OPENXML` is a rowset provider, `OPENXML` can be used in [!INCLUDE[tsql](../../includes/tsql-md.md)] statements in which rowset providers such as a table, view, or the `OPENROWSET` function can appear.
@@ -53,64 +53,64 @@ Indicates the mapping used between the XML data and the relational rowset, and h
53
53
54
54
| Byte value | Description |
55
55
| --- | --- |
56
-
|**0**| Defaults to **attribute-centric** mapping. |
57
-
|**1**| Use the **attribute-centric** mapping. Can be combined with XML_ELEMENTS. In this case, **attribute-centric** mapping is applied first. Next, **element-centric** mapping is applied for any remaining columns. |
58
-
|**2**| Use the **element-centric** mapping. Can be combined with XML_ATTRIBUTES. In this case, **attribute-centric** mapping is applied first. Next, **element-centric** mapping is applied for any remaining columns. |
59
-
|**8**| Can be combined (logical OR) with XML_ATTRIBUTES or XML_ELEMENTS. In the context of retrieval, this flag indicates that the consumed data shouldn't be copied to the overflow property `@mp:xmltext`. |
56
+
|`0`| Defaults to `attribute-centric` mapping. |
57
+
|`1`| Use the `attribute-centric` mapping. Can be combined with `XML_ELEMENTS`. In this case, `attribute-centric` mapping is applied first. Next, `element-centric` mapping is applied for any remaining columns. |
58
+
|`2`| Use the `element-centric` mapping. Can be combined with `XML_ATTRIBUTES`. In this case, `element-centric` mapping is applied first. Next, `attribute-centric` mapping is applied for any remaining columns. |
59
+
|`8`| Can be combined (logical OR) with `XML_ATTRIBUTES` or `XML_ELEMENTS`. In the context of retrieval, this flag indicates that the consumed data shouldn't be copied to the overflow property `@mp:xmltext`. |
60
60
61
-
#### SchemaDeclaration
61
+
#### *SchemaDeclaration*
62
62
63
-
Is the schema definition of the form: *ColNameColType*[*ColPattern* | *MetaProperty*][ , *ColNameColType*[*ColPattern* | *MetaProperty*] ... ]
63
+
A schema definition of the form: *ColNameColType*[*ColPattern* | *MetaProperty*][ , *ColNameColType*[*ColPattern* | *MetaProperty*] ... ]
64
64
65
65
-*ColName*
66
66
67
67
The column name in the rowset.
68
68
69
69
-*ColType*
70
70
71
-
The [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] data type of the column in the rowset. If the column types differ from the underlying **xml** data type of the attribute, type coercion occurs.
71
+
The [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] data type of the column in the rowset. If the column types differ from the underlying **xml** data type of the attribute, type coercion occurs.
72
72
73
73
-*ColPattern*
74
74
75
-
An optional, general XPath pattern that describes how the XML nodes should be mapped to the columns. If *ColPattern* isn't specified, the default mapping (**attribute-centric** or **element-centric** mapping as specified by *flags*) takes place.
75
+
An optional, general XPath pattern that describes how the XML nodes should be mapped to the columns. If *ColPattern* isn't specified, the default mapping (`attribute-centric` or `element-centric` mapping as specified by *flags*) takes place.
76
76
77
-
The XPath pattern specified as *ColPattern* is used to specify the special nature of the mapping (for **attribute-centric** and **element-centric** mapping) that overwrites or enhances the default mapping indicated by *flags*.
77
+
The XPath pattern specified as *ColPattern* is used to specify the special nature of the mapping (for `attribute-centric` and `element-centric` mapping) that overwrites or enhances the default mapping indicated by *flags*.
78
78
79
79
The general XPath pattern specified as *ColPattern* also supports the metaproperties.
80
80
81
81
-*MetaProperty*
82
82
83
-
One of the metaproperties provided by OPENXML. If *MetaProperty* is specified, the column contains information provided by the metaproperty. The metaproperties allow you to extract information (such as relative position and namespace information) about XML nodes. These metaproperties provide more information than is visible in the textual representation.
83
+
One of the metaproperties provided by `OPENXML`. If *MetaProperty* is specified, the column contains information provided by the metaproperty. The metaproperties allow you to extract information (such as relative position and namespace information) about XML nodes. These metaproperties provide more information than is visible in the textual representation.
84
84
85
85
#### *TableName*
86
86
87
87
The table name that can be given (instead of *SchemaDeclaration*), if a table with the desired schema already exists and no column patterns are required.
88
88
89
89
## Remarks
90
90
91
-
The WITH clause provides a rowset format (and additional mapping information as required) by using either *SchemaDeclaration* or specifying an existing *TableName*. If the optional WITH clause isn't specified, the results are returned in an **edge** table format. Edge tables represent the fine-grained XML document structure (such as element/attribute names, the document hierarchy, the namespaces, PIs, and so on) in a single table.
91
+
The `WITH` clause provides a rowset format (and additional mapping information as required) by using either *SchemaDeclaration* or specifying an existing *TableName*. If the optional `WITH` clause isn't specified, the results are returned in an **edge** table format. Edge tables represent the fine-grained XML document structure (such as element/attribute names, the document hierarchy, the namespaces, PIs, and so on) in a single table.
92
92
93
93
The following table describes the structure of the **edge** table.
94
94
95
95
| Column name | Data type | Description |
96
96
| --- | --- | --- |
97
-
|**id**|**bigint**| The unique ID of the document node.<br /><br />The root element has an ID value 0. The negative ID values are reserved. |
98
-
|**parentid**|**bigint**| Identifies the parent of the node. The parent identified by this ID isn't necessarily the parent element, but it depends on the NodeType of the node whose parent is identified by this ID. For example, if the node is a text node, the parent of it may be an attribute node.<br /><br />If the node is at the top level in the XML document, its **ParentID** is NULL. |
99
-
|**nodetype**|**int**| Identifies the node type. Is an integer that corresponds to the XML DOM node type numbering.<br /><br />The node types are:<br /><br />1 = Element node<br /><br />2 = Attribute node<br /><br />3 = Text node |
100
-
|**localname**|**nvarchar**| Gives the local name of the element or attribute. Is NULL if the DOM object doesn't have a name. |
101
-
|**prefix**|**nvarchar**| The namespace prefix of the node name. |
102
-
|**namespaceuri**|**nvarchar**| The namespace URI of the node. If the value is NULL, no namespace is present. |
103
-
|**datatype**|**nvarchar**| The actual data type of the element or attribute row, otherwise is NULL. The data type is inferred from the inline DTD or from the inline schema. |
104
-
|**prev**|**bigint**| The XML ID of the previous sibling element. Is NULL if there is no direct previous sibling. |
105
-
|**text**|**ntext**| Contains the attribute value or the element content in text form (or is NULL if the **edge** table entry doesn't require a value). |
97
+
|`id`|**bigint**| The unique ID of the document node.<br /><br />The root element has an ID value `0`. The negative ID values are reserved. |
98
+
|`parentid`|**bigint**| Identifies the parent of the node. The parent identified by this ID isn't necessarily the parent element, but it depends on the `nodetype` of the node whose parent is identified by this ID. For example, if the node is a text node, the parent of it might be an attribute node.<br /><br />If the node is at the top level in the XML document, its `ParentID` is `NULL`. |
99
+
|`nodetype`|**int**| Identifies the node type. This value is an integer that corresponds to the XML DOM node type numbering.The node types are:<br /><br />`1` = Element node<br />`2` = Attribute node<br />`3` = Text node |
100
+
|`localname`|**nvarchar**| Gives the local name of the element or attribute. `NULL` if the DOM object doesn't have a name. |
101
+
|`prefix`|**nvarchar**| The namespace prefix of the node name. |
102
+
|`namespaceuri`|**nvarchar**| The namespace URI of the node. If the value is `NULL`, no namespace is present. |
103
+
|`data type`|**nvarchar**| The actual data type of the element or attribute row, otherwise is `NULL`. The data type is inferred from the inline DTD or from the inline schema. |
104
+
|`prev`|**bigint**| The XML ID of the previous sibling element. `NULL` if there's no direct previous sibling. |
105
+
|`text`|**ntext**| Contains the attribute value or the element content in text form (or is `NULL` if the **edge** table entry doesn't require a value). |
106
106
107
107
## Examples
108
108
109
109
### A. Use a basic SELECT statement with OPENXML
110
110
111
111
The following example creates an internal representation of the XML image by using `sp_xml_preparedocument`. A `SELECT` statement that uses an `OPENXML` rowset provider is then executed against the internal representation of the XML document.
112
112
113
-
The *flag* value is set to `1`. This value indicates **attribute-centric** mapping. Therefore, the XML attributes map to the columns in the rowset. The *rowpattern* specified as `/ROOT/Customer` identifies the `<Customers>` nodes to be processed.
113
+
The *flag* value is set to `1`. This value indicates `attribute-centric` mapping. Therefore, the XML attributes map to the columns in the rowset. The *rowpattern* specified as `/ROOT/Customer` identifies the `<Customers>` nodes to be processed.
114
114
115
115
The optional *ColPattern* (column pattern) parameter isn't specified because the column name matches the XML attribute names.
If the same `SELECT` statement is executed with *flags* set to `2`, indicating **element-centric** mapping, the values of `CustomerID` and `ContactName` for both of the customers in the XML document are returned as NULL, because there aren't any elements named `CustomerID` or `ContactName` in the XML document.
157
+
If the same `SELECT` statement is executed with *flags* set to `2`, indicating `element-centric` mapping, the values of `CustomerID` and `ContactName` for both of the customers in the XML document are returned as `NULL`, because there aren't any elements named `CustomerID` or `ContactName` in the XML document.
### B. Specify ColPattern for mapping between columns and the XML attributes
168
+
### B. Specify *ColPattern* for mapping between columns and the XML attributes
169
169
170
170
The following query returns customer ID, order date, product ID, and quantity attributes from the XML document. The *rowpattern* identifies the `<OrderDetails>` elements. `ProductID` and `Quantity` are the attributes of the `<OrderDetails>` element. However, `OrderID`, `CustomerID`, and `OrderDate` are the attributes of the parent element (`<Orders>`).
171
171
@@ -175,7 +175,7 @@ The optional *ColPattern* is specified for the following mappings:
175
175
176
176
- The `ProdID` column in the rowset maps to the `ProductID` attribute, and the `Qty` column in the rowset maps to the `Quantity` attribute of the nodes identified in *rowpattern*.
177
177
178
-
Although the **element-centric** mapping is specified by the *flags* parameter, the mapping specified in *ColPattern* overwrites this mapping.
178
+
Although the `element-centric` mapping is specified by the *flags* parameter, the mapping specified in *ColPattern* overwrites this mapping.
The sample XML document in the following example consists of `<Customers>`, `<Orders>`, and `<Order_0020_Details>` elements. First, `sp_xml_preparedocument` is called to obtain a document handle. This document handle is passed to `OPENXML`.
227
227
228
-
In the `OPENXML` statement, the *rowpattern* (`/ROOT/Customers`) identifies the `<Customers>` nodes to process. Because the WITH clause isn't provided, `OPENXML` returns the rowset in an **edge** table format.
228
+
In the `OPENXML` statement, the *rowpattern* (`/ROOT/Customers`) identifies the `<Customers>` nodes to process. Because the `WITH` clause isn't provided, `OPENXML` returns the rowset in an **edge** table format.
229
229
230
230
Finally the `SELECT` statement retrieves all the columns in the **edge** table.
231
231
@@ -258,6 +258,6 @@ SELECT * FROM OPENXML(@idoc, '/ROOT/Customers')
258
258
EXEC sp_xml_removedocument @idoc;
259
259
```
260
260
261
-
## See also
261
+
## Related content
262
262
263
263
-[Examples: Using OPENXML](../../relational-databases/xml/examples-using-openxml.md)
0 commit comments