XML - Managing Data Exchange/Exchanger XML Lite

From Wikibooks, open books for an open world
Jump to: navigation, search



Xml book cover wiki.png XML - Managing Data Exchange
Chapters
Appendices
Exercises
Related Topics
Computer Science Home
Library and Information Science Home
Markup Languages
Get Involved
To do list
Contributors list
Contributing to Wikibooks
Previous Chapter Next Chapter
XQuery XML and JDBC



Exchanger XML Lite[edit]

Cladonia offers an xml editor at http://www.exchangerxml.com/ for free noncommercial use, and can be downloaded without registration.

This is a Java-based product that runs on all platforms including Windows, Linux, Mac OSX and UNIX.

(NOTE: If you need an XML editor for commercial use, you can get a free 30-day trial of Exchanger XML Professional at http://www.exchangerxml.com)

Single Entity in Exchanger XML Lite[edit]

The following directions will lead you step-by-step through doing the same project that is found in the XML - Managing Data Exchange/A single entity chapter.

Part One: Creating the Project Folder[edit]

1) Open Exchanger XML Lite

2) Click on:

           -Project
            -New Project : a "New Project" folder will appear 
                                in the project folder window

3) Type "TourGuide" over the "New Project" title to change the name of the new project to TourGuide.

Exchange2.png

Part Two: Creating the Schema File[edit]

1) Click on:

           -File
            -New 
             -For Type
              -Scroll to "XML Schema Definition" and highlight it
               -OK

2)Exchanger automatically puts the beginning and ending tags in the file for you, however, for our example, delete those automatic tags, and copy and paste the following code into the file:

<?xml version="1.0" encoding="UTF-8"?>
 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> 
    <!-- Tour Guide -->
    <!--The tourGuide element is defined as a complex element type, i.e. 
        it has embedded elements.--> 
    <xsd:element name="tourGuide">
        <xsd:complexType>
            <xsd:sequence>
                <!--The minimum number of times an element can occur is 
                 set using minOccurs (default is 1) and an unlimited 
                number of times an element can occur maxOccurs=”unbounded”.-->
                <xsd:element name="city" type="cityDetails" minOccurs = "1" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
     </xsd:element>
     <!-- City -->
     <!--City is declared an element of named complex type -->
     <!--<xsd:complexType begins the declaration of a complex  
type, ”cityDetails” identifies the complex type. 
     This is NOT the name of the element. This complex type 
     definition may be used in the declarations of more than one element.-->
     <xsd:complexType name="cityDetails">
        <!--Schema element sequence {13} specifies that the child elements 
        must appear in the order specified.-->
        <xsd:sequence>
             <!--<xsd:element begins the declaration of an element of simple 
             type. ”cityName” is the name of the element being declared (in 
             the XML document it will look something like: <cityName> ) 
             and ”xsd:string” is the data type of the element.--> 
             <xsd:element name="cityName" type="xsd:string"/>
             <xsd:element name="adminUnit" type="xsd:string"/>
             <xsd:element name="country" type="xsd:string"/>
             <xsd:element name="population" type="xsd:integer"/>
             <xsd:element name="area" type="xsd:integer"/>
             <xsd:element name="elevation" type="xsd:integer"/>
             <xsd:element name="longitude" type="xsd:decimal"/>
             <xsd:element name="latitude" type="xsd:decimal"/>
             <xsd:element name="description" type="xsd:string"/>
             <xsd:element name="history" type="xsd:string"/>
         <!--Closing of tags. Note: these should not overlap.--> 
         </xsd:sequence>
     </xsd:complexType>
  </xsd:schema>

3) Click on the GREEN CHECK to Validate, and the BROWN CHECK to check for Well-Formedness. These can be found on the toolbar: Exchanger1.png

(NOTE: Be sure to eliminate any "white space" before the text that you paste, or you may have an error when validating.)

4)Click on:

    -File
     -Save
      -"city.xsd"

5)Right Click on:

     -"TourGuide" project folder
       -Add File
        -click on "city.xsd"
         -open
      
  (Note: Now the project "TourGuide" should contain one file,
        "city.xsd".)

Part Three: Creating the Style Sheet[edit]

1)Click on:

    -File
     -New
      -For Type
       -Scroll to "XML StyleSheet Language" and highlight it
        -OK

2)Delete any automatic tags that appear, and cut and paste the following code into the file:

<?xml version="1.0" encoding="UTF-8"?>
  <!--<xsl:stylesheet> declares start of stylesheet and identifies the version 
     number and the official W3C namespace.-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <!--<xsl:template> defines the start of a template and contains 
    rules to apply when a specified node is matched. The match attribute is 
    used to associate (match) the template with an XML element, in this case 
    the root (/), or whole branch, of the XML source document. The XSL looks 
    for the root match and then outputs the HTML.--> 
    <xsl:template match="/">
        <!--The contents of the template element are placed in the output 
         stream of HTML that the browser will be able to interpret.-->
        <html>
            <head>
                <title>Tour Guide</title>
            </head>
            <body>
               <h2>Cities</h2>
                <xsl:apply-templates select="tourGuide"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="tourGuide">
        <xsl:for-each select="city">
            <xsl:text>City: </xsl:text>
                <!--<xsl:value-of> extracts the value of the selected 
                node/XML element and adds it to the output stream-->
                <xsl:value-of select="cityName"/>
                <br/>
            <xsl:text>Population: </xsl:text>
                <xsl:value-of select="population"/>
                <br/>
            <xsl:text>Country: </xsl:text>
                <xsl:value-of select="country"/>
                <br/>
                <br/>
        </xsl:for-each>
    <!--<xsl:for-each> can be used to loop through each node in a specified 
         node set and add them to the output-->     
    </xsl:template>
 </xsl:stylesheet>

3) Click on the GREEN CHECK to Validate, and the BROWN CHECK to check for Well-Formedness. (NOTE: Be sure to eliminate any "white space" before the text that you paste, or you may have an error when validating.)

4)Click on:

     -File
      -Save As
       -"city.xsl"

5)Right Click on:

     -"TourGuide" project folder
      -Add File
       -"city.xsl"
         -open
     (Note: Now the project "TourGuide" contains two files, 
      "city.xsd", and "city.xsl".)

Part Four: Creating the XML File[edit]

1) Click on:

   -File
    -New
     -Default XML Document
      -OK

2) Delete any automatic tags that appear and copy and paste the following code:

 <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="city.xsl" type="text/xsl"?>

<!--The following declaration identifies the root element of the document 
(tourGuide) and the schema file (city.xsd) using xsi=schemaLocation-->
<tourGuide>
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='city.xsd'>
    <!--The definition of the first city-->
    <city>
        <cityName>Belmopan</cityName>
        <adminUnit>Cayo</adminUnit>
        <country>Belize</country>
        <population>11100</population>
        <area>5</area>
        <elevation>130</elevation>
        <longitude>88.44</longitude>
        <latitude>17.27</latitude>
        <description>Belmopan is the capital of Belize</description>
        <history>Belmopan was established following the devastation of the
           former capitol, Belize City, by Hurricane Hattie in 1965. High 
           ground and open space influenced the choice and ground-breaking 
           began in 1966.  By 1970 most government offices and operations had 
           already moved to the new location.
        </history>
    </city>
    <!--the definition of the second city-->
    <city>
        <cityName>Kuala Lumpur</cityName>
        <adminUnit>Selangor</adminUnit>
        <country>Malaysia</country>
        <population>1448600</population>
        <area>243</area>
        <elevation>111</elevation>
        <longitude>101.71</longitude>
        <latitude>3.16</latitude>
        <description>Kuala Lumpur is the capital of Malaysia and the largest 
            city in the nation</description>
        <history>The city was founded in 1857 by Chinese tin miners and  
            perseded Klang.  In 1880 the British government transferred their 
            headquarters from Klang to Kuala Lumpur, and in 1896 it became the 
            capital of Malaysia. 
        </history>
    </city>
<!--The closing of the root element tag-->    
</tourGuide>

3) Click on the GREEN CHECK to Validate, and the BROWN CHECK to check for Well-Formedness. (NOTE: Be sure to eliminate any "white space" before the text that you paste, or you may have an error when validating.)

(Also NOTE: You may need to select 
  -Schema
   -Infer XML Schema
    -then choose city.xsd
in order to validate the xml file.)

4)Click on:

  -File
   -Save As
    -city.xml

5) Right click on:

   -TourGuide
    -Add File
     -"city.xml"
      -open
(Note: Now project "TourGuide" should contain three files, 
 "city.xsd","city.xsl", and "city.xml".)

Part Five: Executing your code[edit]

1) Open the city.xml file.

2) Click on:

  -Transform
   -Execute Simple XSLT
    -Current Document
     -OK
     -XSL input
      -From URl
       -pick city.xsl
        -open
          -OK
         -Use Default Processor
          -OK
Note: the window should say "Transformation Complete"
Now you may close this window and follow step 3 to get the results.

3)Click on:

  -Tools
   -Start Browser
    
 Note:  Results should look like this:

Result.png