# Understanding the Transformation

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:

1. The fifth line of the query is shown in the following listing:

    <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.

2. The sixth line of the query is shown in the following 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:

1. The <name> and </name> tags transform directly to XML output.
2. Characters between curly braces {} 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>

3. The seventh line in the query is shown in the following listing:

    <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.

1. The <address> and </address> tags transform directly to XML output.
2. Characters between curly braces {} 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.
3. The 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 Following XPath Expression | Returns | The String
--- | --- | ---
$priceQuoteDoc/ns0:shipAddress/@ns0:street | The value of the street attribute of the shipAddress element. | 12 Springs Rd
$priceQuoteDoc/ns0:shipAddress/@ns0:city | The value of the city attribute of the shipAddress element. | Morris Plains
$priceQuoteDoc/ns0:shipAddress/@ns0:state | The value of the state attribute of the shipAddress element. | nj
$priceQuoteDoc/ns0:shipAddress/@ns0:zip | The value of the zip attribute of the shipAddress element. | 07960

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>

4. The last line of the query is shown in the following listing:

    </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.