Using Annotated XDR Schemas in Queries


There are three ways to query annotated XDR schemas to generate XML documents. One we've used already, and the other two we haven't dealt with until now. We'll talk first about the method we've already used.

Template Files Utilizing XPath Queries

This is the method we've been using up to this point.We specify a <sql:xpath-query> element in a template file that enables us to use XPath queries and post them against the view created by an XDR schema.Within the <sql:xpath-query> element, we utilize the mapping-schema attribute to specify the XDR schema that the XPath query is posted against. XPath queries and the FOR XML extension return document fragments to the template. The template enables us to apply a ROOT element to the returned fragment and thereby make the fragment a valid XML document.

Inline Mapping Schemas

Now, for a slightly new way of doing things, let's put the XDR schema and the template together in the same file. That's right, together in the same file. There's really nothing difficult here, just a slightly different syntax (famous last words, right?). We add the sql:is-mapping-schema annotation to the <SCHEMA> portion of the file. This annotation takes a Boolean value (0 = false, 1 = true). Also specified within the <SCHEMA> element is the ID type attribute annotation sql:id . The value assigned to the sql:id annotation references the matching mapping-schema value of the sql:xpath-query element and executes the XPath query there.

Listing 5.61 shows an example of what is called an inline mapping schema. Listing 5.62 shows the partial results. All we need to execute it is a URL, which is http://iisserver/Nwind/templates/InlineMappingTemplate.xml .

Be sure to place the inline mapping schema file in the template's virtual directory. By default, that's where the file is looked for. Putting the file in the schema's virtual directory results in a Page Not Found error.

Listing 5.61 An Inline Mapping Schema
 <ROOT     xmlns:sql="urn:schemas-microsoft-com:xml-sql">    <Schema xmlns="urn:schemas-microsoft-com:xml-data"      sql:id="MappingSchema"      sql:is-mapping-schema="1">      <ElementType name="Orders" >        <AttributeType name="OrderID" />        <AttributeType name="CustomerID" />        <AttributeType name="OrderDate" />        <attribute type="OrderID" />        <attribute type="CustomerID" />        <attribute type="OrderDate" />      </ElementType>    </Schema>  <sql:xpath-query mapping-schema="#MappingSchema">    Orders  </sql:xpath-query>  </ROOT> 
Listing 5.62 Partial Results From the Inline Mapping Schema
 <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">    <Orders OrderID="10248" CustomerID="VINET" OrderDate="1996-07-    04T00:00:00" />    <Orders OrderID="10249" CustomerID="TOMSP" OrderDate="1996-07-    05T00:00:00" />    <Orders OrderID="10250" CustomerID="HANAR" OrderDate="1996-07-    08T00:00:00" />  ...  </ROOT> 

Mapping Schema in the URL

The third and final method of querying XDR annotated schema is placing the XPath query directly in the URL. To accomplish this, create a schema file and place it in the schema virtual directory. Then, in the calling URL, specify the schema file name and the XPath query used. Listing 5.63 gives you a simple schema for this example.

The following is the calling URL we'll use:

 http://iisserver/Nwind/schemas/URLSchema.xml/Orders[@OrderID="10250"]. 

This dictates that the file name is URLSchema.xml and that it exists in the schema's virtual directory.

Listing 5.63 A Schema to Test Querying Via a URL
 <?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="Orders">      <AttributeType name="CustomerID" />      <AttributeType name="Name" />      <AttributeType name="Address" />      <AttributeType name="City" />      <attribute type="CustomerID" />      <attribute type="Name" sql:field="ShipName" />      <attribute type="Address" sql:field="ShipAddress" />      <attribute type="City" sql:field="ShipCity" />    </ElementType>  </Schema> 

This is the result of our mapping schema in a URL:

 <Orders OrderID="10250" Name="Hanari Carnes" Address="Rua do Pao, 67"  City="Rio de Janeiro" /> 

Be sure to place the mapping schema file in the schema's virtual directory. By default, that's where the file is looked for. Putting the file in the template's virtual directory results in a Page Not Found error.



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net