Post Reply 
 
Thread Rating:
  • 392 Votes - 2.85 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML2:Transforming XML
06-16-2011, 09:59 AM (This post was last modified: 06-16-2011 10:49 AM by kino.)
Post: #1
XML2:Transforming XML
The last Curl Cue addressed how to access XML data from a server and store the information as an XML document model (XDMNodes). Now the content must be transformed or processed to meet the application functionality. Since the data has already been internalized, all we need to do is:

•query the relevant information,

•perform relevant processesing,

•and diplay the requested information to the user.





One of the most common types of display is a grid. In the first example, we query data and dislay it using a standard Table and in the second example we will display it using a RecordGrid.


Querying the Data
Once the data is contained within the Curl XML DOM structure, the goal is to extract elements and their associated values by traversing the node tree. This includes element types, element values, and element attributes. Using the WSDK, there are three different ways to obtain this information. Either by using object methods, XPath expressions, or elaboration. In this Curl Cue, we will focus on using XPath expressions to query relevant content.


We will use the same data from the last Curl Cue (XML Data). The following is the XDMDocument view so that you can recall the structure of the XML.

Code:
{curl 6.0, 7.0 applet}
{applet manifest = "manifest.mcurl"}
{import * from COM.CURL.WSDK.XML-DOCUMENT-MODEL}
{import * from COM.CURL.WSDK.XML-DISPLAY}

{value
   let xml-data:Url ={url "services.xml"}

   let xmldoc:XDMDocument =
       {build-xml preserve-whitespace? = false, xml-data}

    {XDMTreeControl open?=true, xmldoc}
}

Using XPath to Query Data


All of the nodes in the XDMDocument can be queried using an XPath expression. Nodes include XDMElement,XDMAttribute, XDMText, and XDMProcessingInstruction.


XPath expressions are used to traverse the nodes in the XML structure and extract elements and their associated values. XPath queries return an XPathValue which can be one of the following types:

•XDMNodeSet

•String

•double

•bool



In our example, a XDMNodeSet is first created to gather all Sample nodes.
Code:
def items:XDMNodeSet = {model.search Samples/Sample}


Then, standard XPATH expressions are used to query relevant data.

•item.search "@xx" returns an attribute value of xx

•item.search "xx" returns an element value of xx



For more information regarding XPath, please see XPath Curl Cue: About XPath.


Tabular Display
In the first example, we will be extracting data from th displaying relevant XML data in a table. This is an example of transforming the data for presentation.


All XPATH queries will return an XDMNodeSet, therefore we need to convert it to a String before it is able to be used in the table. For example:
Code:
{item.search title}.as-String


Code:
{curl 6.0, 7.0 applet}
{applet manifest = "manifest.mcurl"}
{import * from COM.CURL.WSDK.XML-DOCUMENT-MODEL}
{import * from COM.CURL.WSDK.XML-DISPLAY}


{value
   let xml-data:Url ={url "samples.xml"}
   let model:XDMDocument =
       {build-xml preserve-whitespace? = false, xml-data}
  
   let display:Table =
       {Table
           columns = 2,
           cell-border-width = 1px,
           cell-border-color = "brown",
           cell-border-style = "flat"}
||--    def items:XDMNodeSet = {model.search "Samples/Sample"}
   def items:XDMNodeSet = {model.root.search "sample"}
   {for item in items do
       {display.add
           {row-prototype
               {item.search "@id"}.as-String,
               {item.search "title"}.as-String,
               {link href =
                   {url  {item.search "doclink"}.as-String},
                   {value  {item.search "title"}.as-String}}
           }}}
   display
}

Record Display


In the next example, we are using the XPath to query the title, description and link. The information is stored in a RecordSet and displayed to the user with a RecordDisplay. This is an example of transforming the data into a model for further processing. The model data is then transformed for presentation in to a grid display.

Code:
{curl 6.0, 7.0 applet}
{applet manifest = "manifest.mcurl"}
{import * from COM.CURL.WSDK.XML-DOCUMENT-MODEL}
{import * from COM.CURL.WSDK.XML-DISPLAY}


{value
   let xml-data:Url ={url "samples.xml"}
   let model:XDMDocument =
       {build-xml preserve-whitespace? = false, xml-data}
  
   let display:Table =
       {Table
           columns = 2,
           cell-border-width = 1px,
           cell-border-color = "brown",
           cell-border-style = "flat"
       }

   def items:XDMNodeSet = {model.search "samples/sample"}

   let rs-samples:RecordSet =
       {RecordSet
           {RecordFields
               {RecordField
                   "Title", domain = String
               },
               {RecordField
                   "Description", domain = String
               },
               {RecordField
                   "Link", domain = String
               }
           }
       }

   {for item in items do
       {rs-samples.append
           {RecordData Title =  {item.search "title"}.as-String,
               Description = {item.search "description"}.as-String,
               Link = {item.search "doclink"}.as-String}
       }
   }

   {RecordGrid
       height = 3cm,
       width = 30cm,
       record-source = rs-samples
   }
}
For more information about RecordSets, please see RecordSets.
Find all posts by this user
Quote this message in a reply
Post Reply 

Forum Jump:


User(s) browsing this thread:
1 Guest(s)