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:
{-- requestquote/MyTutorialJoin.dtf#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" <ns2:quote> <name>{ data($priceQuoteDoc/ns0:customerName) }</name> <address>{ concat($priceQuoteDoc/ns0:shipAddress/@street , $priceQuoteDoc/ns0:shipAddress/@city , $priceQuoteDoc/ns0:shipAddress/@state , $priceQuoteDoc/ns0:shipAddress/@zip) }</address> </ns2:quote>
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($priceQuoteDoc/ns0: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:
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($priceQuoteDoc/ns0:shipAddress/@street, $priceQuoteDoc/ns0:shipAddress/@city, $priceQuoteDoc/ns0:shipAddress/@state, $priceQuoteDoc/ns0:shipAddress/@zip) }</address>
The following steps describe the transformation that occurs on this line of XQuery code.
The value of the street attribute of the shipAddress element. |
||
The value of the state attribute of the shipAddress element. |
||
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.
![]() |
![]() |