Tutorial: Building Your First Data Transformation

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Understanding the Concepts

This section is optional and provides detailed conceptual information about the following topics:

 


Understanding the Transformation

The transformation occurring in the query built in Step 3: Mapping Elements and Attributes is shown in the following figure:

Figure 6-1 Mapping Elements and Attributes

Mapping Elements and Attributes

The query generated in To Map Attributes of an Element to Single Element is shown in the following listing:

1. (:: pragma bea:dtfFile-class type="requestquote.MyTutorialJoin" ::)
2. declare namespace xf = "http://tempuri.org/Tutorial_Process_Application_Web/src/requestquote/myJoin/";
3. declare namespace ns0 = "http://www.example.org/price";
4. declare namespace ns1 = "http://www.example.org/avail";
5. declare namespace ns2 = "http://www.example.org/quote";
6. declare function xf:myJoin($priceQuote1 as element(ns0:priceQuote),
7. $availQuote1 as element(ns1:availQuote),
8. $taxRate as xs:float)
9. as element(ns2:quote) {
10. <ns2:quote>
11. <name>{ data($priceQuote1/ns0:customerName) }</name>
12. <address>{ concat($priceQuote1/ns0:shipAddress/@street , $priceQuote1/ns0:shipAddress/@city , $priceQuote1/ns0:shipAddress/@state , $priceQuote1/ns0:shipAddress/@zip) }</address>
13. </ns2:quote>
14. };
15. declare variable $priceQuote1 as element(ns0:priceQuote) external;
16. declare variable $availQuote1 as element(ns1:availQuote) external;
17. declare variable $taxRate as xs:float external;
18. xf:myJoin($priceQuote1,$availQuote1,$taxRate)
Note: The line numbers are provided are for your reference.

The second line of this query represents the namespace of the XQuery function. The 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 tenth line of the query is shown in the following listing:
  2. <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.

  3. The eleventh line of the query is shown in the following listing:
  4. <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.
    3. 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>
  5. The twelfth line in the query is shown in the following listing:

<address>

{

concat($priceQuote1/ns1:shipAddress/@street ,",",

$priceQuote1/ns1:shipAddress/@city ,",",

fn:upper-case($priceQuote1/ns1:shipAddress/@state) ,",",

$priceQuote1/ns1: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 Table 6-1.
    4. Table 6-1 Arguments: XPath Expressions
      The Following XPath Expression
      Returns
      The String
      $priceQuote1/ns0:sh
      ipAddress/@ns0:street
      The value of the street attribute of the shipAddress element.
      12 Springs Rd
      $priceQuote1/ns0:shipAddress/@ns0:city
      The value of the city attribute of the shipAddress element
      Morris Plains
      $priceQuote1/ns0:shipAddress/@ns0:state
      The value of the state attribute of the shipAddress element.
      nj
      $priceQuote1/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>
  1. The last line of the query is shown in the following listing:
  2. </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.

 


Understanding XML Repeating Nodes

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

To View the Full listing of the XML Schema, Open the PriceQuote.xsd file
  1. In the Package Explorer pane, expand the expand the Tutorial_Process_Application_Utility/schemas folder.
  2. Double-click the PriceQuote.xsd icon.
  3. The PriceQuote.xsd file is displayed.

  4. Return to the Design view of the myJoin.xq file:
    1. In the Package Explorer pane, double-click Tutorial_Process_Application_Web\src\requestquote\myJoin.xq
    2. Select the Design tab.

  Back to Top       Previous  Next