跳轉到內容

XML - 管理資料交換/Exchanger XML Lite

來自華夏公益教科書



上一章 下一章
XQuery XML 和 JDBC



Exchanger XML Lite

[編輯 | 編輯原始碼]

Cladonia 免費提供一個 xml 編輯器,可在 http://www.exchangerxml.com/ 免費用於非商業用途,無需註冊即可下載。

這是一個基於 Java 的產品,可在所有平臺上執行,包括 Windows、Linux、Mac OSX 和 UNIX。

(注意:如果您需要用於商業用途的 XML 編輯器,您可以在 http://www.exchangerxml.com 免費試用 30 天 Exchanger XML Professional)

Exchanger XML Lite 中的單個實體

[編輯 | 編輯原始碼]

以下說明將逐步引導您完成與 XML - 管理資料交換/單個實體 章中相同的專案。

第一部分:建立專案資料夾

[編輯 | 編輯原始碼]

1) 開啟 Exchanger XML Lite

2) 點選

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

3) 在“新專案”標題上鍵入“TourGuide”,將新專案的名稱更改為 TourGuide。

第二部分:建立架構檔案

[編輯 | 編輯原始碼]

1) 點選

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

2) Exchanger 會自動在檔案中為您新增開始和結束標籤,但是,對於我們的示例,請刪除這些自動標籤,並將以下程式碼複製貼上到檔案中

<?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) 點選綠色複選標記進行驗證,並點選棕色複選標記檢查是否格式良好。這些可以在工具欄中找到:

(注意:請確保在您貼上的文字之前消除任何“空白”,否則您在驗證時可能會出現錯誤。)

4) 點選

  -File
  -Save
   -"city.xsd"

5) 右鍵點選

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

第三部分:建立樣式表

[編輯 | 編輯原始碼]

1) 點選

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

2) 刪除出現的任何自動標籤,並將以下程式碼剪下貼上到檔案中

<?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) 點選綠色複選標記進行驗證,並點選棕色複選標記檢查是否格式良好。(注意:請確保在您貼上的文字之前消除任何“空白”,否則您在驗證時可能會出現錯誤。)

4) 點選

  -File
   -Save As
   -"city.xsl"

5) 右鍵點選

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

thumhttp://xpressvds.blogspot.com/bnail

第四部分:建立 XML 檔案

[編輯 | 編輯原始碼]

1) 點選

 -File
  -New
  -Default XML Document
   -OK

2) 刪除出現的任何自動標籤,並將以下程式碼複製貼上

<?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) 點選綠色複選標記進行驗證,並點選棕色複選標記檢查是否格式良好。(注意:請確保在您貼上的文字之前消除任何“空白”,否則您在驗證時可能會出現錯誤。)

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

4) 點選

 -File
 -Save As
  -city.xml

5) 右鍵點選

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

第五部分:執行程式碼

[編輯 | 編輯原始碼]

1) 開啟 city.xml 檔案。

2) 點選

 -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) 點選

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

華夏公益教科書