![]() ![]() ![]() ![]() ![]() ![]() |
This section is optional and provides detailed conceptual information about the following topics:
The transformation occurring in the query built in Step 3: Mapping Elements and Attributes is shown in the following figure:
The query generated in To Map Attributes of an Element to Single Element is shown in the following listing:
(:: pragma bea:dtfFile-class type="requestquote.MyTutorialJoin" ::)
declare namespace xf = "http://tempuri.org/RQ_web/src/requestquote/myJoin/";
declare namespace ns0 = "http://www.example.org/price";
declare namespace ns1 = "http://www.example.org/avail";
declare namespace ns2 = "http://www.example.org/quote";
declare function xf:myJoin($priceQuote1 as element(ns0:priceQuote),
$availQuote1 as element(ns1:availQuote),
<name>{ data($priceQuote1/ns0:customerName) }</name>
<address>{ concat($priceQuote1/ns0:shipAddress/@street , $priceQuote1/ns0:shipAddress/@city , $priceQuote1/ns0:shipAddress/@state , $priceQuote1/ns0:shipAddress/@zip) }</address>
declare variable $priceQuote1 as element(ns0:priceQuote) external;
declare variable $availQuote1 as element(ns1:availQuote) external;
declare variable $taxRate as xs:float external;
The first three lines of this query are namespace declarations. These namespace declarations are part of the query prolog. For each namespace in the source and target XML Schema, the mapper generates a namespace declaration. For example, the mapper generates the namespace declaration: ns0
for the namespace URI (http://www.example.org/price) defined in the XML Schema of the PriceQuote.xsd
file. Namespaces are used to uniquely distinguish elements in XML Schema from elements in another XML Schema.
The following steps describe the transformation that occurs when the source XML data is run against the preceding query:
<ns2:quote>
This line of the query becomes the first line of the XML output, as shown in the following listing:
<quot:quote xmlns:quot="http://www.example.org/quote"
>
During the transformation, the namespace prefix for the quote
element changes. In the query, the namespace prefix associated with http://www.example.org/quote
namespace URI is ns2
. However, in the resulting XML data, the namespace prefix generated for the http://www.example.org/quote
namespace URI is quot
. This namespace declaration is highlighted in bold in the preceding listing.
<name>{data($priceQuote1ns0:customerName)}</name>
This line of the query transforms the customerName
element of the priceQuote
element to the name
element of the quote
element.
The following steps describe the transformation that occurs on this line of XQuery code:
<name>
and </name>
tags transform directly to XML output. {}
are interpreted in a special way by the XQuery engine. That is, characters surrounded by curly braces are not transformed directly into XML. Specifically, in this example, the curly braces surrounding the data method specify that the data
function of the XQuery language should be executed.
The data
function returns the value of the passed in XML node. For this example, the argument to the data
function is the following XPath expression: $priceQuoteDoc/ns0:customerName
. The $priceQuoteDoc
variable contains the contents of the priceQuote element, including its subelements. This XPath expression returns the customerName
node of the priceQuote
element. (The /
XPath operator delineates parent nodes from child nodes.)
The XQuery data
function takes customerName
node and returns the value of the node, the string: Acme Inc
. This string is placed between the <name>
and </name>
tags resulting in the following line of output XML data, as shown in the following listing:
<name>Acme Inc</name>
<address>{ concat($priceQuote1/ns0:shipAddress/@street ,
$priceQuote1/ns0:shipAddress/@city , $priceQuote1/ns0:shipAddress/@state ,
$priceQuote1/ns0:shipAddress/@zip) }</address>
The following steps describe the transformation that occurs on this line of XQuery code.
<address>
and </address>
tags transform directly to XML output.{}
are interpreted in a special way by the XQuery engine. That is, characters surrounded by curly braces are not transformed directly into XML. Specifically, in this example, the curly braces surrounding the data method specify that the data
function of the XQuery language should be executed.concat
function takes the values of all its arguments, concatenates these values together, and returns them as a string. For this example, the concat
function takes the values of the all the XPath expressions and concatenates them together in one address string. Additionally, all the arguments in this concat
function are XPath expressions that return the value of specified attribute, as shown in the following table.
The return string of the concat
function is placed between the <address>
and <address>
tags resulting in the following line of XML data, as shown in the following listing:
<address>12 Springs RdMorris Plainsnj07960</address>
</ns2:quote>
The last line of the query becomes the last line of the XML output, as shown in the following listing:
</quot:quote>
The resulting address
element has no delimiter between the street, city, state, and zip code fields, making the address difficult to read and parse. For instructions on adding delimiters to this query, return to
To Edit and Retest the Simple Query in the main section of this tutorial.
A repeating node means that more than one instance of this node can be specified. For example, in the following XML data there are three instances of the priceRequest node, as shown in the following listing:
<?xml version="1.0"?>
<priceQuote xmlns="http://www.example.org/price">
<customerName>Acme Inc</customerName>
<shipAddress street="12 Springs Rd" city="Morris Plains" state="nj" zip="07960"/>
<priceRequests><priceRequest>
</priceRequests>
<widgetId>12</widgetId>
<price>1.00</price>
</priceRequest>
<priceRequest>
<widgetId>134</widgetId>
<price>34.10</price>
</priceRequest>
<priceRequest>
<widgetId>211</widgetId>
<price>10.00</price>
</priceRequest>
</priceQuote>
A segment of the XML Schema for the preceding XML data is shown in the following listing:
<?xml version="1.0"?>
<xsd:schema . . . >
. . .
<xsd:element name="widgetId" type="xsd:integer"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="priceRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="pri:widgetId"/>
<xsd:element ref="pri:price"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="priceRequests">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="pri:priceRequest"minOccurs="1" maxOccurs="10"
/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
. . .
<xsd:element name="priceQuote">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="pri:customerName" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="pri:shipAddress" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="pri:priceRequests"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The minOccurs="1"
and maxOccurs="10"
settings, in the definition of the priceRequest
element (highlighted in bold in the preceding listing), specify that there can be one to ten instances of the priceRequest
element. This defines priceQuote
as a repeating element.
![]() ![]() ![]() |