Home >Backend Development >XML/RSS Tutorial >Create an XML view using an annotated XDR schema
XML views of relational data can be created using the XDR (xml-Data simplified) schema. These views can then be queried using XPath queries. This is similar to using the CREATE VIEW statement to create a view and specify a SQL query against the view.
XML Schema describes the structure of an XML document and the different constraints on the data in the document. When you specify an XPath query against a schema, the structure of the returned XML document is determined by the schema against which the XPath query was performed.
In Microsoft® SQL Server™ 2000, use the simplified XML-Data (XDR) language to create the schema. XDR is a flexible language that overcomes some of the limitations of document type definitions (DTDs) used to describe the structure of documents. Unlike DTDs, XDR schemas describe document structure using the same syntax as XML documents. Additionally, in a DTD, all data content is character data. The XDR language schema enables you to specify the data type of an element or attribute.
In XDR Schema, the 1de00b41ae8be4b3c03a631e9569276b element contains the entire schema. As attributes of the 1de00b41ae8be4b3c03a631e9569276b element, you can describe the attributes that define the name of the schema and the namespace in which the schema resides. In the XDR language, all element declarations must be enclosed in a 1de00b41ae8be4b3c03a631e9569276b element.
The minimal XDR schema is as follows:
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data"> ... </Schema> <Schema> 元素是从 xml-data 命名空间 (urn:schemas-microsoft-com:xml-data) 派生出的。
Description This document assumes that you are familiar with the XML-Data language.
Annotations for XDR Schema
You can use annotations in the XDR schema that describe the mapping to the database to query the database and return the results in an XML document format. SQL Server 2000 introduced a number of annotations that can be used to map XDR schemas to tables and columns in the database. You can specify XPath queries on XML views created by an XDR schema to query the database and obtain results in XML format.
This is an alternative to the more complex SQL query writing process, which uses the FOR XML EXPLICIT pattern to describe the XML document structure as part of the query. For more information about using FOR XML EXPLICIT mode in SELECT queries, see Using EXPLICIT Mode. However, to overcome most limitations of XPath queries on mapping schemas, use FOR XML EXPLICIT mode for SQL queries that return results in XML document format.
If you have a public XDR schema (such as the Microsoft BizTalk™ schema), you can do any of the following:
· Write a query in FOR XML EXPLICIT mode so that the generated data is valid for the public XDR schema; however, write FOR XML EXPLICIT queries can be cumbersome.
· Make a private copy of a public XDR schema. The annotations are then added to the private replica, resulting in a mapping schema. You can specify an XPath query for the mapping schema. The query produces data in the common schema namespace. Creating an annotated schema and specifying an XPath query against that schema is a much simpler process than writing complex FOR XML EXPLICIT queries. The image below illustrates this process.
Description The Microsoft BizTalk™ framework is designed to define a standard XML format for common business objects such as contracts, orders, and appointments. Copies of these business architectures can be found at http://biztalk.org/BizTalk/default.asp.
Mapping Schema
In the context of a relational database, it is very useful to map an arbitrary XDR schema to a relational store. One way to achieve this is to annotate XDR schemas. The annotated XDR schema is called a "mapping schema" and provides information about how XML data is mapped to a relational store. A mapping schema is actually an XML view of relational data. You can use these mappings to retrieve relational data in XML document format.
Microsoft SQL Server 2000 introduced a number of annotations that can be used in XDR schemas to map elements and attributes to database tables and columns. You can use XPath (XML path) to specify queries to the mapping schema (XML view). The mapping schema describes the resulting document structure.
Namespace of annotations
In XDR schema, use the following namespace to specify annotations: urn:schemas-microsoft-com:xml-sql.
The following example shows that the simplest way to specify a namespace is to specify it in the 1de00b41ae8be4b3c03a631e9569276b tag. urn:schemas-microsoft-com:xml-sql Namespace annotations must be namespace qualified.
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:sql="urn:schemas-microsoft-com:xml-sql" > ........... </Schema>
The namespace prefix used is arbitrary. In this documentation, the sql prefix is used to denote an annotation namespace and to distinguish annotations in this namespace from annotations in other namespaces.
Namespace of data types
The XDR schema allows you to specify the data type of an element or attribute. Use the following namespace to specify data types: urn:schemas-microsoft-com:datatypes.
The following is the minimal XDR schema with namespace declaration:
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns:dt="urn:schemas-microsoft-com:datatypes"> ... </Schema>
所用的命名空间前缀是任意的。 在本文档中,dt 前缀用于表示数据类型命名空间和使此命名空间中的批注区别于其它命名空间中的批注。
1de00b41ae8be4b3c03a631e9569276b 元素来源于 xml-data 命名空间:urn:schemas-microsoft-com:xml-data。
XDR 架构示例
下例显示如何将批注添加到 XDR 架构中。XDR 架构由 a7c15504e26c2dc9d33fa17ec3230b5f 元素和 EmpID、Fname 及 Lname 特性组成。
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <ElementType name="Employee" > <AttributeType name="EmpID" /> <AttributeType name="FName" /> <AttributeType name="LName" /> <attribute type="EmpID" /> <attribute type="FName" /> <attribute type="LName" /> </ElementType> </Schema>
现在,将批注添加到此 XDR 架构中,使架构的元素和特性映射到数据库的表和列。 带批注的 XDR 架构如下:
<?xml version="1.0" ?> <Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <ElementType name="Employee" sql:relation="Employees" > <AttributeType name="EmpID" /> <AttributeType name="FName" /> <AttributeType name="LName" /> <attribute type="EmpID" sql:field="EmployeeID" /> <attribute type="FName" sql:field="FirstName" /> <attribute type="LName" sql:field="LastName" /> </ElementType> </Schema>
在此映射架构中,使用 sql:relation 批注将 a7c15504e26c2dc9d33fa17ec3230b5f 元素映射到 Employees 表。使用 sql:field 批注将特性 EmpID、Fname 和 Lname 映射到 Employees 表中的 EmployeeID、FirstName 和 LastName 列。
此带批注的 XDR 架构提供关系数据的 XML 视图。使用 Xpath(XML 路径)语言可以查询该 XML 视图。Xpath 查询返回 XML 文档形式的结果,而不是 SQL 查询所返回的行集。
说明 在映射架构中,指定的关系值(如表名和列名)区分大小写。
以上就是使用带批注的 XDR 架构创建 XML 视图的内容,更多相关内容请关注PHP中文网(www.php.cn)!