Tutorial OpenXML

April 26, 2018 | Author: api-26214845 | Category: X Path, Xml, Xml Schema, Parsing, Namespace
Share Embed Donate


Short Description

Download Tutorial OpenXML...

Description

©2008 Microsoft Corporation. All rights reserved. Transact-SQL Reference (SQL Server 2000)

OPENXML OPENXML provides a rowset view over an XML document. Because OPENXML is a rowset provider, OPENXML can be used in Transact-SQL statements in which rowset providers such as a table, view, or the OPENROWSET function can appear. Syntax OPENXML( OPENXML(idoc int idoc int [in], in],rowpattern rowpattern nvarchar[ nvarchar[in],[ in],[flags flags byte[ byte[in]] in]]) ) [WITH (SchemaDeclaration (SchemaDeclaration | TableName)] TableName)] Arguments idoc  Is the document handle of the internal representation of an XML document. The internal representation of  an XML document is created by calling sp_xml_preparedocument sp_xml_preparedocument.. rowpattern Is the XPath pattern used to identify the nodes (in the XML document whose handle is passed in the idoc  parameter) to be processed as rows. flags Indicates the mapping that should be used between the XML data and the relational rowset, and how the spill-over column should be filled. flags is an optional input parameter, and can be one o f these values. Byte Valu Value e

Desc Descri ript ptio ion n

0

Defaults to attribute-centric mapping.

1

Use the attribute-centric mapping. Can be combined with XML_ELEMENTS; in which case, attribute-centric mapping is applied first, and then element-centric mapping is applied for all columns not yet dealt with.

2

Use the element-centric mapping. Can be combined with XML_ATTRIBUTES; in which case, attribute-centric mapping is applied first, and then element-centric mapping is applied for all columns not yet dealt with.

8

Can be combined (logical OR) with XML_ATTRIBUTES or XML_ELEMENTS. In context of retrieval, this flag indicates that the consumed data should not be copied to the overflow property @mp:xmltext. @mp:xmltext.

SchemaDeclaration Is the schema definition of the form: ColName ColType [ColPattern | MetaProperty ][, ][, ColName ColType [ColPattern | MetaProperty ]...] ]...] ColName Is the column name in the rowset. ColType

Is the SQL 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. If the column is of type timestamp, timestamp, the present value in the XML document is disregarded when selecting from an OPENXML rowset, and the autofill values are returned. ColPattern Is an optional, general XPath pattern that describes how the XML nodes should be mapped to the columns. If the ColPattern is not specified, the default mapping ( attribute-centric or element-centric mapping as specified by flags) flags) takes place. The XPath pattern specified as ColPattern is used to specify the special nature of the mapping (in case of  attribute-centric and element-centric mapping) that overwrites or enhances the default mapping indicated by flags. flags. The general XPath pattern specified as ColPattern also supports the metaproperties. MetaProperty  Is one of the metaproperties provided by OPENXML. If the metaproperty is specified, the column contains information provided by the metaproperty. The metaproperties allow you to extract information (such as relative position, , namespace information) about XML nodes, which provides more information than is visible in the textual representation. TableName Is the table name that can be give n (instead of SchemaDeclaration of  SchemaDeclaration)) if a table with the desired schema already exists and no column patterns are required. The WITH clause provides a rowset format (and additional mapping information as necessary) using either SchemaDeclaration or specifying an existing TableName. TableName. If the optional WITH clause is not specified, the results are returned in an edge table format. Edge tables represent the fine-grained XML document structure (e.g. element/attribute names, the document hierarchy, the namespaces, PIs etc.) in a single table. This table describes the structure of the edge table . Column name

Data type

Description

id

bigint

Is the unique ID of the document node. The root element has an ID value 0. The negative ID values are reserved.

parentid

bigint

Identifies the parent of the node. The parent identified by this ID is not 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. If the node is at the top level in the XML document, its ParentID is NULL.

nodetype

int

Identifies the node type. Is an integer that corresponds to the XML DOM node type numbering (see DOM for node information). The node types are: 1 = Element node 2 = Attribute node 3 = Text node

localname

nvarchar

Gives the local name of the ele ment or attribute. Is NULL if the DOM object does not have a name.

prefix

nvarchar

Is the namespace prefix of the node name.

name namesp spac ace euri uri

nvarch archar ar

Is the namespace URI of the node. I f the value is NULL, no namespace is present.

datatype

nvarchar

Is the actual data type of the ele ment or attribute row and is NULL

otherwise. The data type is inferred from the inline DTD or from the inline schema. prev

bigint

Is the XML ID of the previous sibling element. Is NULL if there is no direct previous sibling.

text

ntext

Contains the attribute value or the element content in text form (or is NULL if the edge table entry does not need a value).

Examples A. Use a simple SELECT statement with OPENXML This example creates an internal representation of the XML image using sp_xml_preparedocument sp_xml_preparedocument.. A SELECT statement using an OPENXML rowset provider is then executed against the internal representation of the XML document. The flag value is set to 1 indicating attribute-centric mapping. Therefore, the XML attributes map to the columns in the rowset. The rowpattern specified as /ROOT/Customers identifies the nodes to be processed. The optional ColPattern (column pattern) is not specified because the column name matches the XML attribute names. The OPENXML rowset provider creates a two-column rowset ( CustomerID and ContactName) ContactName) from which the SELECT statement retrieves the necessary columns (in this case, all the columns).

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc =' ' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement that uses the OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Custo '/ROOT/Customer',1) mer',1) WITH (CustomerID varchar(10), ContactName varchar(20)) Here is the result set:

CustomerID ---------VINET LILAS

ContactName -------------------Paul Henriot Carlos Gonzlez

If the same SELECT statement is executed with flags set to 2, indicating element-centric mapping, the values of CustomerID of CustomerID and ContactName for both of the customers in the XML document are returned as NULL, because the elements do not have any subelements. Here is the result set:

CustomerID ---------NULL NULL

ContactName ----------NULL NULL

B. Specify ColPattern for mapping between columns and the XML attributes This query returns customer ID, order date, product ID and quantity attributes from the XML document. The rowpattern identifies the elements. ProductID and Quantity are the attributes of the element. However, the OrderID, OrderID, CustomerID and OrderDate are the attributes of the parent element (). The optional ColPattern is specified, indicating that: •

The OrderID, OrderID, CustomerID and OrderDate in the rowset map to the attributes of the parent of 

the nodes identified by rowpattern in the XML document.



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. rowpattern. Although the element-centric mapping is specified by the flags parameter, the mapping specified in ColPattern overwrites this mapping.

declare @idoc int declare @doc varchar(1000) set @doc =' ' --Create an internal representation of the XML document. exec sp_xml_preparedocument @idoc OUTPUT, @doc

-- SELECT stmt using OPENXML rowset provider SELECT * FROM OPENXML (@idoc, '/ROOT/Custome '/ROOT/Customer/Order/OrderD r/Order/OrderDetail',2) etail',2) WITH (OrderID int '../@OrderID', '../@OrderID ', CustomerID varchar(10) '../@CustomerI '../@CustomerID', D', OrderDate datetime '../@OrderDate', '../@OrderDate ', ProdID int '@ProductID', Qty int '@Quantity') This is the result: OrderID CustomerID

OrderDate

ProdID

Qty

------------------------------------------------------------------------

10248 10248 10283

VINET VINET LILAS

1996-07-04 00:00:00.000 1996-07-04 00:00:00.000 1996-08-16 00:00:00.000

11 42 72

12 10 3

C. Obtain result in an edge table format In this example, the WITH clause is not specified in the OPENXML statement. As a result, the rowset generated by OPENXML has an edge table format. The SELECT statement returns all the columns in the edge table. The sample XML document in the example consists of , , and elements. First sp_xml_preparedocument is called to obtain a document handle. This document handle is p assed to OPENXML. In the OPENXML statement •

The rowpattern (/ROOT/Customers) identifies the nodes to process.



The WITH clause is not provided. Therefore OPENXML returns the rowset in an edge table format.

Finally the SELECT statement retrieves all the columns in the edge table.

declare @idoc int declare @doc varchar(1000) set @doc =' ' --Create an internal representation of the XML document. exec sp_xml_preparedocument @idoc OUTPUT, @doc -- SELECT statement using OPENXML rowset provider SELECT * FROM OPENXML (@idoc, '/ROOT/Custo '/ROOT/Customers') mers') EXEC sp_xml_removedocument @idoc The result is returned as an edge table. See Also Using OPENXML [ http://msdn2.microsoft.com/en-us/library/aa226522(SQL.80,printer).aspx ]

©2008 Microsoft Corporation. All rights reserved. XML and Internet Support (SQL Server 2000)

Using OPENXML The examples in this topic show how OPENXML is used in creating a rowset view of an XML document. For information about the syntax of OPENXML, see OPENXML [ http://msdn2.microsoft.com/enus/library/aa276847(SQL.80,printer).aspx ] . The examples show all aspects of OPENXML e xcept specifying metaproperties in OPENXML. For more information about specifying metaproperties in OPENXML, see Specifying Metaproperties in OPENXML [ http://msdn2.microsoft.com/enus/library/aa226531(SQL.80,printer).aspx ] . Examples In retrieving the data, rowpattern is used to identify the nodes in the XML document that determine the rows. rowpattern is expressed in the XPath pattern language used in the MSXML XPath imple mentation. For example, if the pattern ends in an element or an attribute, a row is created for each element or attribute node selected by rowpattern. rowpattern. The flags value provides default mapping. In the SchemaDeclaration, SchemaDeclaration, if no ColPattern is specified, the mapping specified in flags is assumed. The flags value is ignored if ColPattern if ColPattern is specified in SchemaDeclaration. SchemaDeclaration. The specified ColPattern determines the mapping (attribute-centric ( attribute-centric or elementcentric) centric) and also the behavior in dealing with overflow and unconsumed data. A. Execute a simple SELECT statement with OPENXML The XML document in this example consists of the < Customer>, , and elements. The OPENXML statement retrieves customer information in a two-column rowset ( CustomerID and ContactName) ContactName) from the XML document. First, the sp_xml_preparedocument stored procedure is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement: •

rowpattern (/ROOT/Customer) identifies the nodes to process.



The flags parameter value is set to 1 indicating attribute-centric mapping. As a result, the XML

attributes map to the columns in the rowset defined in SchemaDeclaration. SchemaDeclaration.



In SchemaDeclaration (in the WITH clause), the specified ColName values match the corresponding

XML attribute names. Therefore, the ColPattern parameter is not specified in SchemaDeclaration. SchemaDeclaration. And then, the SELECT statement retrieves all the columns in the rowset provided by OPENXML.

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc ='

' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer',1) WITH (CustomerID varchar(10), ContactName varchar(20)) EXEC sp_xml_removedocument @idoc This is the result:

CustomerID ---------VINET LILAS

ContactName -------------------Paul Henriot Carlos Gonzlez

If the same SELECT statement is executed with flags set to 2, indicating element-centric mapping, because elements do not have any subelements, the values of CustomerID of  CustomerID and ContactName for both the customers are returned as NULL. If in the XML document, the < CustomerID> and are subelements, the element-centric mapping retrieves the values.

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc =' VINET Paul Henriot LILAS Carlos Gonzlez '

-- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Custom '/ROOT/Customer',2) er',2) WITH (CustomerID varchar(10), ContactName varchar(20)) EXEC sp_xml_removedocument @idoc This is the result:

CustomerID ---------VINET LILAS

ContactName -------------------Paul Henriot Carlos Gonzlez

B. Specify ColPattern for mapping between rowset columns and the XML attributes/elements This example shows how the XPath pattern is specified in the optional ColPattern parameter to provide mapping between rowset columns and the XML attributes (and elements). The XML document in this example consists of the < Customer>, , and elements. The OPENXML statement retrieves customer and order information as a rowset ( CustomerID, CustomerID, OrderDate, OrderDate, ProdID, ProdID, and Qty) Qty) from the XML document. First, the sp_xml_preparedocument stored procedure is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement: •

rowpattern (/ROOT/Customer/Order/OrderDetail) identifies the nodes to process.



For illustration purposes, the flags parameter value is set to 2 indicating element-centric mapping.

However, the mapping specified in ColPattern overwrites this mapping (the XPath pattern specified in ColPattern maps the columns in the rowset to attributes thus resulting in an attribute-centric mapping). In SchemaDeclaration (in the WITH clause), ColPattern is also specified with the ColName and ColType parameters. The optional ColPattern is the XPath pattern specified to indicate: •

The OrderID, OrderID, CustomerID, CustomerID, and OrderDate columns in the rowset map to the attributes of the

parent of the nodes identified by rowpattern. rowpattern. rowpattern identifies the nodes. Therefore, the CustomerID and OrderDate columns map to CustomerID and OrderDate attributes of the element.



The ProdID and Qty columns in the rowset map to the ProductID and Quantity attributes of the

nodes identified in rowpattern. rowpattern. And then the SELECT statement retrieves all the columns in the rowset provided by OPENXML.

DECLARE @idoc int DECLARE @doc varchar(1000)

SET @doc =' ' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT stmt using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2) WITH (OrderID int '../@OrderID', '../@OrderID' , CustomerID varchar(10) '../@Customer '../@CustomerID', ID', OrderDate datetime '../@OrderDate', '../@OrderDat e', ProdID int '@ProductID', Qty int '@Quantity') This is the result:

OrderID CustomerID OrderDate ProdID Qty ------------------------------------------------------------10248 VINET 1996-07-04 00:00:00.000 11 12 10248 VINET 1996-07-04 00:00:00.000 42 10 10283 LILAS 1996-08-16 00:00:00.000 72 3 The XPath pattern specified as ColPattern can also be specified to map the XML elements to the rowset columns (resulting in element-centric mapping). In the following example, the XML document and are subelements of element. Because ColPattern overwrites the mapping specified in flags parameter, the flags parameter is not specified in OPENXML.

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc =' 10248 VINET 1996-07-04T00:00:00

10283 LILAS 1996-08-16T00:00:00 ' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT stmt using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail') WITH (CustomerID varchar(10) '../CustomerID', '../CustomerI D', OrderDate datetime '../OrderDate', '../OrderDate ', ProdID int '@ProductID', Qty int '@Quantity') EXEC sp_xml_removedocument @idoc C. Combining attribute-centric and element-centric mapping In this example, the flags parameter is set to 3, indicating that both attribute-centric and elementcentric mapping is to be applied. In this case, the attribute-centric mapping is applied first, and then element-centric mapping is applied for all the columns not yet dealt with.

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc =' Paul Henriot Carlos Gonzlez ' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer',3) WITH (CustomerID varchar(10), ContactName varchar(20))

EXEC sp_xml_removedocument @idoc This is the result

CustomerID ---------VINET LILAS

ContactName -------------------Paul Henriot Carlos Gonzlez

The attribute-centric mapping is applied for CustomerID. CustomerID. There is no ContactName attribute in the element; therefore, element-centric element-centric mapping is applied. D. Specify text() XPath function as ColPattern The XML document in this example consists of the < Customer> and elements. The OPENXML statement retrieves a rowset consisting of the oid attribute from the element, the ID of the parent of the node (identified by rowpattern), rowpattern), and the leaf-value string of the element content. First, the sp_xml_preparedocument stored procedure is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement: •

rowpattern (/root/Customer/Order) identifies the nodes to process.



The flags parameter value is set to 1, indicating attribute-centric mapping. As a result, the XML

attributes map to the rowset columns defined in SchemaDeclaration. SchemaDeclaration.



In SchemaDeclaration (in the WITH clause), the rowset column names, oid and amount, amount, match the

corresponding XML attribute names. Therefore, the ColPattern parameter is not specified. For the comment column in the rowset, the XPath function ( text()) text()) is specified as ColPattern. ColPattern. This overwrites the attribute-centric mapping specified in flags, flags, and the column contains the leaf-value string of the element content. And then, the SELECT statement retrieves all the columns in the rowset provided by OPENXML.

DECLARE @idoc int DECLARE @doc varchar(1000) --sample XML document SET @doc =' Customer was very satisfied Important Happy Customer.

' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/root/Customer/Order', 1) WITH (oid char(5), amount float, comment ntext 'text()') EXEC sp_xml_removedocument @idoc This is the result:

oid ----O1 O2 O3 O4

amount ----------3.5 13.4 100.0 10000.0

comment ------------ -------------------------------------------NULL Customer was very satisfied Happy Customer. NULL

E. Specify TableName in the WITH clause This example specifies TableName in the WITH clause instead of SchemaDeclaration of  SchemaDeclaration in the WITH clause. This is useful if you have a table with the structure you want and no column patterns ( ColPattern parameter) are required. The XML document in this example consists of the < Customer> and elements. The OPENXML statement retrieves order information in a three-column rowset ( oid, oid, date, date, and amount) amount) from the XML document. First, the sp_xml_preparedocument stored procedure is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement: •

rowpattern (/root/Customer/Order) identifies the nodes to process.



There is no SchemaDeclaration in the WITH clause. Instead, a table name is specified. Therefore,

the table schema is used as the rowset schema.



The flags parameter value is set to 1, indicating attribute-centric mapping. Therefore, attributes

of the elements (identified by rowpattern) rowpattern) map to the rowset columns with the same name. And then the SELECT statement retrieves all the columns in the rowset provided by OPENXML.

-- Create a test table. This table schema is used by OPENXML as the -- rowset schema. CREATE TABLE T1(oid char(5), date datetime, amount float) DECLARE @idoc int DECLARE @doc varchar(1000)

-- Sample XML document SET @doc =' Customer was very satisfied Important ' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/root/Customer/Order', 1) WITH T1 EXEC sp_xml_removedocument @idoc This is the result:

oid ----O1 O2 O3 O4

date --------------------------1996-01-20 00:00:00.000 1997-04-30 00:00:00.000 1999-07-14 00:00:00.000 1996-01-20 00:00:00.000

amount ---------3.5 13.4 100.0 10000.0

F. Obtain the result in an edge table format In this example, the WITH clause is not specified in the OPENXML statement. As a result, the rowset generated by OPENXML has an edge table format. The SELECT statement returns all the columns in the edge table. The sample XML document in the example consists of the , , and elements. First, the sp_xml_preparedocument stored procedure is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement: •

rowpattern (/ROOT/Customer) identifies the nodes to process.



The WITH clause is not provided; therefore, OPENXML returns the rowset in an edge table format.

And then the SELECT statement retrieves all the columns in the edge table.

DECLARE @idoc int DECLARE @doc varchar(1000) SET @doc =' ' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer') EXEC sp_xml_removedocument @idoc The result is returned as an edge table. You can write queries against the edge table to obtain information: •

The following query returns the number of  Customer nodes in the document. Because the WITH

clause is not specified, OPENXML returns an edge table. The SELECT statement queries the edge table.





SELECT count(*)



FROM OPENXML(@idoc, '/')



WHERE localname = 'Customer'

This query returns local names of XML nodes of element type. •

SELECT distinct localname



FROM OPENXML(@idoc, '/')



WHERE nodetype = 1



ORDER BY localname

G. Specify rowpattern ending with an attribute The XML document in this example consists of the < Customer>, , and elements. The OPENXML statement retrieves order details information in a three-column rowset ( ProductID, ProductID, Quantity, Quantity, and OrderID) OrderID) from the XML document. First, the sp_xml_preparedocument is called to obtain a document handle. This document handle is passed to OPENXML. In the OPENXML statement:



rowpattern (/ROOT/Customer/Order/OrderDetail/@ProductID) ends with an XML attribute

(ProductID). ProductID). In the resulting rowset, a row is created for each attribute node selected in the XML document.



In this example, the flags parameter is not specified. Instead, the mappings are specified by the

ColPattern parameter. In SchemaDeclaration (in the WITH clause), ColPattern is also specified with the ColName and ColType parameters. The optional ColPattern is the XPath pattern specified to indicate: •

The XPath pattern (. ( .) specified as ColPattern for the ProdID column in the rowset identifies the

context node (current node). As per the rowpattern specified, it is the ProductID attribute of the element.



The ColPattern, ColPattern, ../@Quantity, ../@Quantity, specified for the Qty column in the rowset identifies the Quantity

attribute of the parent () node of the context node ().



Similarly, the ColPattern, ColPattern, ../../@OrderID, ../../@OrderID, specified for the OID column in the rowset identifies the

OrderID attribute of the parent () of the parent () node of the context node (). And then, the SELECT statement retrieves all the columns in the rowset provided by OPENXML.

DECLARE @idoc int DECLARE @doc varchar(1000) --Sample XML document SET @doc =' ' -- Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT stmt using OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail/@ProductID') WITH ( ProdID int '.', Qty int '../@Quantity '../@Quantity', ', OID int '../../@Order '../../@OrderID') ID')

EXEC sp_xml_removedocument @idoc This is the result:

ProdID ----------11 42 72

Qty ----------12 10 3

OID ------10248 10248 10283

H. Specify an XML document with multiple text nodes If you have multiple text nodes in an XML document, a SELECT statement with a ColPattern (text()) text()) returns only the first text node instead of all. For example:

DECLARE @h int EXEC sp_xml_preparedocument @h OUTPUT, N' TaU ', '' SELECT * FROM openxml(@h, '/root/b:Elem') WITH (Col1 varchar(20) 'text()') The SELECT statement returns T as the result (and not TaU) TaU) I. Retrieve individual values from m ultivalued attributes An XML document can have attributes that are multivalued. For example the IDREFS attribute can be multivalued. In an XML document, the multivalued attribute values are specified as a string with the values separated by a space. In the follow ing XML document, the attends attribute of the element and the attendedBy attribute of are multivalued. Retrieving individual values from a multivalued XML attribute and storing each value in a separate row in the database requires additional work. This example shows the process. This sample XML document consists of the following elements: •



Consists of id of id (student ID), name, name, and attends attributes. The attends attribute is a multivalued attribute.





Consists of id of id (class ID), name, name, and attendedBy attributes. The attendedBy attribute is a multivalued attribute. This attends attribute in and the attendedBy attribute in represent a m:n relationship between Student and Class tables. A student can take many classes and a class can have many students.

Assume you want to shred this document and save it in the database as follows: •

Save the data in the Students table.



Save the data in the Courses table.



Save he m:n relationship data (between Student and Class) in the CourseAttendence table.

Additional work is required to e xtract the values. To retrieve this information and store it in the table , use these stored procedures: Insert_Idrefs_Values



Inserts the values of course ID and student ID in the CourseAttendence table. Extract_idrefs_values



Extracts the individual student IDs from each element. An edge table is used to retrieve these values. Here are the steps: 1.

Crea Create te the the fol follo lowin wing g tabl tables es:: 2.

DROP TABLE CourseAttendance

3.

DROP TABLE Students

4.

DROP TABLE Courses

5.

GO

6.

CREATE TABLE Students(

7.

id

varchar(5) primary key,

8.

name varchar(30)

9.

)

10.

GO

11.

CREATE TABLE Courses(

12.

id

varchar(5) primary key,

13.

name

varchar(30),

14.

taughtBy varchar(5)

15.

)

16.

GO

17.

CREATE TABLE CourseAttendance(

18.

id

19.

attendedBy varchar(5) references

Students(id),

varchar(5) references Courses(id),

constraint CourseAttendance_PK primary key

20.

(id, attendedBy) 21.

)

22.

go

23. Create Create these stored stored procedur procedures: es: 24.

DROP PROCEDURE f_idrefs

25.

GO

26.

CREATE PROCEDURE f_idrefs

27.

@t

varchar(500),

28.

@idtab

varchar(50),

29.

@id

varchar(5)

30.

AS

31.

DECLARE @sp int

32.

DECLARE @att varchar(5)

33.

SET @sp = 0

34.

WHILE (LEN(@t) > 0)

35.

BEGIN

36.

SET @sp = CHARINDEX(' ', @t+ ' ')

37.

SET @att = LEFT(@t, @sp-1)

38.

EXEC('INSERT INTO '+@idtab+' VALUES ('''+@id+''',

'''+@att+''')') SET @t = SUBSTRING(@t+ ' ', @sp+1, LEN(@t)+1-@sp)

39. 40.

END

41.

Go

42. 43.

DROP PROCEDURE fill_idrefs

44.

GO

45.

CREATE PROCEDURE fill_idrefs

46.

@xmldoc

int,

47.

@xpath

varchar(100),

48.

@from

varchar(50),

49.

@to

varchar(50),

50.

@idtable

varchar(100)

51.

AS

52.

DECLARE @t varchar(500)

53.

DECLARE @id varchar(5)

54.

55.

/* Temporary Edge table */

56.

SELECT *

57.

INTO #TempEdge

58.

FROM OPENXML(@xmldoc, @xpath)

59.

DECLARE fillidrefs_cursor CURSOR FOR

60.

SELECT CAST(iv.text AS nvarchar(200)) AS id,

61.

CAST(av.text AS nvarchar(4000)) AS refs

62.

FROM

63.

#TempEdge c, #TempEdge i, #TempEdge iv, #TempEdge a, #TempEdge av

64. 65.

WHERE

c.id = i.parentid

66.

AND

UPPER(i.localname) UPPER(i.local name) = UPPER(@from)

67.

AND

i.id = iv.parentid

68.

AND

c.id = a.parentid

69.

AND

UPPER(a.localname) UPPER(a.local name) = UPPER(@to)

70.

AND

a.id = av.parentid

71. 72.

OPEN fillidrefs_cursor

73.

FETCH NEXT FROM fillidrefs_ fillidrefs_cursor cursor INTO @id, @t

74.

WHILE (@@FETCH_STATUS -1)

75.

BEGIN

76.

IF (@@FETCH_STATUS -2)

77.

BEGIN execute f_idrefs @t, @idtable, @id

78. 79.

END

80.

FETCH NEXT FROM fillidrefs_cursor INTO @id, @t

81.

END

82.

CLOSE fillidrefs_cursor

83.

DEALLOCATE fillidrefs_cursor

84.

Go

85. This is the sample sample document that is shredded shredded and the data is stored stored in the preceding tables. 86.

DECLARE @h int

87.

EXECUTE sp_xml_preparedocument @h OUTPUT, '

88.

94.



95. 96.



97. 98.



99. 100.



101. 102.

103.



'

108. 109.

INSERT INTO Students SELECT * FROM OPENXML(@h,

'//Student') WITH Students 110. 111.

INSERT INTO Courses SELECT * FROM OPENXML(@h, '//Class')

WITH Courses 112.

/* Using the edge table */

113.

EXECUTE fill_idrefs @h, '//Class', 'id', 'attendedby',

'CourseAttendance' 114. 115.

SELECT * FROM Students

116.

SELECT * FROM Courses

117.

SELECT * FROM CourseAttendance

118. 119.

EXECUTE sp_xml_removedocument @h

See Also sp_xml_preparedocument [ http://msdn2.microsoft.com/en-us/library/aa260385(SQL.80,printer).aspx ] sp_xml_removedocument [ http://msdn2.microsoft.com/en-us/library/aa260386(SQL.80,printer).aspx ] OPENXML [ http://msdn2.microsoft.com/en-us/library/aa276847(SQL.80,printer).aspx ] Writing XML Using OPENXML [ http://msdn2.microsoft.com/en-us/library/aa226536(SQL.80,printer).aspx ]

©2008 Microsoft Corporation. All rights reserved. Transact-SQL Reference (SQL Server 2000)

sp_xml_preparedocument Reads the Extensible Markup Language (XML) text provided as input, then parses the text using the MSXML parser (Msxml2.dll), and provides the parsed document in a state ready for consumption. This parsed document is a tree representation of the various nodes (elements, attributes, text, comments, and so on) in the XML document. sp_xml_preparedocument returns a handle that can be used to access the newly created internal representation of the XML document. This handle is valid for the duration of the connection to Microsoft® SQL Server™ 2000, until the connection is reset, or until the handle is invalidated by executing sp_xml_removedocument.. sp_xml_removedocument Note A parsed document is stored in the internal cache of SQL Server 2000. The MSXML parser uses oneeighth the total memory available for SQL Server. To avoid running out of memory, run sp_xml_removedocument to free up the memory. Syntax sp_xml_preparedocument hdoc OUTPUT hdoc OUTPUT [, xmltext ] [, xpath_namespaces  xpath_namespaces]] Arguments hdoc  Is the handle to the newly created document. hdoc is hdoc is an integer. [ xmltext ] Is the original XML document. The MSXML parser parses this XML do cument.  xmltext is  xmltext is a text (char (char,, nchar, nchar, varchar, varchar, nvarchar, nvarchar, text, text, or ntext) ntext) parameter. The default value is NULL, in which case an internal representation of an empty XML document is created. [ xpath_namespaces  xpath_namespaces]] Specifies the namespace declarations that are used in row and column XPath expressions in OPENXML. The default value is . xmlns:mp="urn:schemas-microsoft-com:xml-metaprop">.  xpath_namespaces provides the namespace URIs for the prefixes used in the XPath expressions in OPENXML by means of a well-formed XML document. xpath_namespaces document. xpath_namespaces declares the prefix that must be used to refer to the namespace urn:schemas-microsoft-com:xml-metaprop urn:schemas-microsoft-com:xml-metaprop,, which provides meta data about the parsed XML elements. Although you can redefine the namespace prefix for the metaproperty namespace using this technique, this namespace is not lost. The prefix mp is still valid for urn:schemas-microsoftcom:xml-metaprop even if  xpath_namespaces  xpath_namespaces contains no such declaration. xpath_namespaces declaration.  xpath_namespaces is a text (char, char, nchar, nchar, varchar, varchar, nvarchar, nvarchar, text, text, or ntext) ntext) parameter. Return Code Values 0 (success) or >0 (failure) Permissions Execute permissions default to the public role.

Examples A. Prepare an internal representation for a well- formed XML document This example returns a handle to the newly created internal representation of the XML document that is provided as input. In the call to sp_xml_preparedocument sp_xml_preparedocument,, a default namespace prefix mapping is used.

DECLARE @hdoc int DECLARE @doc varchar(1000) SET @doc =' ' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc -- Remove the internal representation. exec sp_xml_removedocument @hdoc B. Prepare an internal representation for a well-formed XML document with a DTD This example returns a handle to the newly created internal representation of the XML document that is provided as input. The stored procedure validates the document loaded against the DTD included in the document. In the call to sp_xml_preparedocument sp_xml_preparedocument,, a default namespace prefix mapping is used.

DECLARE @hdoc int DECLARE @doc varchar(2000) SET @doc = ' ]> ' EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc C. Specify a namespace URI

This example returns a handle to the newly created internal representation of the XML document that is provided as input. The call to sp_xml_preparedocument preserves the mp prefix to the metaproperty namespace mapping and adds the xyz mapping prefix to the namespace urn:MyNamespace urn:MyNamespace..

DECLARE @hdoc int DECLARE @doc varchar(1000) SET @doc =' ' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc, '' See Also sp_xml_removedocument [ http://msdn2.microsoft.com/en-us/library/aa260386(SQL.80,printer).aspx ]

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF