萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> XML 程式設計

NetBeans XSLT Designer 快速入門:建立第一個 XSL 轉換服務

教學目標

使用 NetBeans Enterprise Pack 5.5.1 Beta 內建的 XSLT Designer,建立、部署並測試一個簡單的 XSL 轉換 Web 服務:接收訊息 → 依 XSL 樣式表轉換 → 回傳結果。

前置需求

  • NetBeans IDE 5.5.1 RC1
  • NetBeans Enterprise Pack 5.5.1 Beta
  • 基本 NetBeans 操作經驗

環境設定

  1. 開啟 Runtime 視窗 → 展開 Servers → 右鍵 Sun Java System Application Server 9 → Start (若已有綠色三角則略過)。
  2. 展開 Sun Java System Application Server 9 > JBI > Service Engines,確認 XSLT Service Engine 運行中 (若無,右鍵 com.sun.xsltse-1.0-2 → Start)。

建立 XSLT Module 專案

  1. File → New Project
  2. Categories: Service Oriented Architecture → Projects: XSLT Module → Next
  3. 專案名稱:HelloXSLTransformation → Finish

建立 XML Schema (XSD)

需兩個:輸入 HelloXSLTIncoming.xsd、輸出 HelloXSLTOutgoing.xsd。

  1. 右鍵專案 Transformation Files → New → File/Folder → XML → XML Schema
  2. 檔名 HelloXSLTIncoming → Finish
  3. 在 Schema 視圖右鍵 Elements → Add Element,名稱 name,型別 Inline Simple Type → 字串
  4. 同樣建立 HelloXSLTOutgoing.xsd,元素名 greeting,型別字串。

產生的 XSD 片段:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="https://www.w3.org/2001/XMLSchema"
  targetNamespace="https://xml.netbeans.org/schema/HelloXSLTIncoming"
  elementFormDefault="qualified">
  <xsd:element name="name">
    <xsd:simpleType><xsd:restriction base="xsd:string"/></xsd:simpleType>
  </xsd:element>
</xsd:schema>

建立 WSDL 文件

  1. 右鍵 Transformation Files → New → WSDL Document
  2. 檔名 HelloXSLTWSDL → Next
  3. Input Element: 選 HelloXSLTIncoming.xsd → name
  4. Output Element: 選 HelloXSLTOutgoing.xsd → greeting
  5. Binding Type: SOAP → Subtype: Document Literal → Finish

建立 XSL 樣式表

  1. 右鍵 Transformation Files → New → XSL Stylesheet
  2. 檔名 HelloXSLT → Finish
  3. 在設計視圖拖曳輸入 name 到輸出 greeting,雙擊編輯 XPath 運算式:concat('Hello, ', /in:name, '!')

部署與測試

  1. 右鍵專案 → Build → Deploy
  2. 在 Runtime → JBI → Service Assemblies 確認部署成功。
  3. 使用 Web Service Tester (右鍵 WSDL → Test Web Service) 傳入 <name>World</name>,預期收到 <greeting>Hello, World!</greeting>。

後續學習

  • 深入 XPath 2.0 / XSLT 2.0 功能
  • 整合資料庫、訊息隊列等企業級元件
  • 遷移至 NetBeans 6.0+ / Apache ServiceMix 等新平台
copyright © 萬盛學電腦網 all rights reserved