天天看点

用XSLT文件在浏览器格式化显示XML文件

XMLSpy编辑XSD,DTD,XML和XSLT文件非常方便,altova公司还提供了Home Edition免费版提供下载。

首先定义一个描述Component的XML Schema文件

<?xml version="1.0" encoding="UTF-8" ?>

<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by ibm (ibm) -->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:col="http://www.novacolimas.com/colimas/namespace"

       targetNamespace="http://www.novacolimas.com/colimas/namespace" elementFormDefault="qualified"

       attributeFormDefault="unqualified">

       <xs:element name="ComponentsBasicInfo">

              <xs:annotation>

                     <xs:documentation>Title: cbi.xsd

Subject: the component basic information.

                     Publisher: Nova Corporation, Colimas.

Format: text/xml

Creator: Zhao Lei

Date.Created: 2005-04-23

Language: en-US

Description:

Change Log:

Version       Date                     Modifier Description

01.00  2005/04/25        Zhao Lei        Initial release.                                                                        

                     </xs:documentation>

              </xs:annotation>

              <xs:complexType>

                     <xs:sequence>

                           <xs:element name="ComponentBasicInfo" minOccurs="0" maxOccurs="unbounded">

                                  <xs:complexType>

                                         <xs:sequence>

                                                <xs:element name="SerialNo">

                                                       <xs:annotation>

                                                              <xs:documentation>Component SerialNo, 27 characters with a-z or A-Z or 0-9</xs:documentation>

                                                       </xs:annotation>

                                                       <xs:simpleType>

                                                              <xs:restriction base="xs:string">

                                                                     <xs:pattern value="[a-zA-Z0-9]{27}" />

                                                              </xs:restriction>

                                                       </xs:simpleType>

                                                </xs:element>

                                                <xs:element name="Name" type="xs:string">

                                                       <xs:annotation>

                                                              <xs:documentation>Component Name</xs:documentation>

                                                       </xs:annotation>

                                                </xs:element>

                                                <xs:element name="Alias" type="xs:string">

                                                       <xs:annotation>

                                                              <xs:documentation>Component Alias</xs:documentation>

                                                       </xs:annotation>

                                                </xs:element>

                                                <xs:element name="Version" default="1.0">

                                                       <xs:annotation>

                                                              <xs:documentation>Component Version</xs:documentation>

                                                       </xs:annotation>

                                                </xs:element>

                                                <xs:element name="DevelopmentLanguage" type="xs:string">

                                                       <xs:annotation>

                                                              <xs:documentation>Component Development Language</xs:documentation>

                                                       </xs:annotation>

                                                </xs:element>

                                                <xs:element name="DevelopmentPlatform" type="xs:string" />

                                                <xs:element name="Authors" type="xs:string" />

                                                <xs:element name="Status" default="Unconfirmed">

                                                       <xs:annotation>

                                                              <xs:documentation>the status of component: Unconfirmed | Confirmed | Deleted</xs:documentation>

                                                       </xs:annotation>

                                                       <xs:simpleType>

                                                              <xs:restriction base="xs:string">

                                                                     <xs:pattern value="Unconfirmed|Confirmed|Deleted" />

                                                              </xs:restriction>

                                                       </xs:simpleType>

                                                </xs:element>

                                                <xs:element name="ConfirmDate" type="xs:dateTime" />

                                                <xs:element name="ConfirmBy" type="xs:string" />

                                                <xs:element name="CreateDate" type="xs:dateTime" />

                                                <xs:element name="CreateBy" type="xs:string" />

                                                <xs:element name="LastChangeDate" type="xs:dateTime" />

                                                <xs:element name="LastChangeBy" type="xs:string" />

                                                <xs:element name="BrowseTimes" type="xs:integer" default="0" />

                                                <xs:element name="IsPublic" type="xs:boolean" default="true">

                                                       <xs:annotation>

                                                              <xs:documentation>whether the component is public</xs:documentation>

                                                       </xs:annotation>

                                                </xs:element>

                                                <xs:element name="DevelopmentEnvironment" type="xs:string" />

                                                <xs:element name="FunctionDescription" type="xs:string" />

                                                <xs:element name="Developers">

                                                       <xs:simpleType>

                                                              <xs:restriction base="xs:string" />

                                                       </xs:simpleType>

                                                </xs:element>

                                                <xs:element name="Admins">

                                                       <xs:simpleType>

                                                              <xs:restriction base="xs:string" />

                                                       </xs:simpleType>

                                                </xs:element>

                                         </xs:sequence>

                                  </xs:complexType>

                           </xs:element>

                     </xs:sequence>

              </xs:complexType>

       </xs:element>

</xs:schema>

用XMLSpy编辑一个cbi.xsd的xml文件:cbidebug.xml

<?xml version="1.0" encoding="UTF-16"?>

<?xml-stylesheet type="text/xsl" href="component.xslt"?>

<ComponentsBasicInfo xmlns="http://www.novacolimas.com/colimas/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.novacolimas.com/colimas/namespace

C:/DOCUME~1/Administrator/MYDOCU~1/NovaPlan/Colimas/ID/XSD/cbi.xsd">

       <ComponentBasicInfo>

              <SerialNo>aaaaaaaaaaaaaaaaaaaaaaaaaaa</SerialNo>

              <Name>Mactintosh组件</Name>

              <Alias>Mactintosh component</Alias>

              <Version>1.0</Version>

              <DevelopmentLanguage>Java</DevelopmentLanguage>

              <DevelopmentPlatform>Linux</DevelopmentPlatform>

              <Authors>磊 赵, 福锦 黄</Authors>

              <Status>Unconfirmed</Status>

              <ConfirmDate>2001-12-17T09:30:47.0Z</ConfirmDate>

              <ConfirmBy>String</ConfirmBy>

              <CreateDate>2001-12-17T09:30:47.0Z</CreateDate>

              <CreateBy>String</CreateBy>

              <LastChangeDate>2001-12-17T09:30:47.0Z</LastChangeDate>

              <LastChangeBy>String</LastChangeBy>

              <BrowseTimes>123</BrowseTimes>

              <IsPublic>true</IsPublic>

              <DevelopmentEnvironment>WSAD,XML, Websphere, DB2, WebSphere MQ, AIX.CPU is Intel</DevelopmentEnvironment>

              <FunctionDescription>Component Function Description.</FunctionDescription>

              <Developers>Lei Zhao, Herry Chow</Developers>

              <Admins>Lei Zhao</Admins>        

       </ComponentBasicInfo>

</ComponentsBasicInfo>

接着定义XSLT文件:component.xslt

其中<xsl:variable name="componentinfo.cominfo" select="'Component Information'"/>

的select值可以更换为其他语言。

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:col="http://www.novacolimas.com/colimas/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:variable name="componentinfo.cominfo" select="'Component Information'"/>

<xsl:variable name="template.edit" select="'Edit!'"/>

<xsl:variable name="template.down" select="'Download'"/>

<xsl:variable name="template.confirm" select="'Confirm!'"/>

<xsl:variable name="template.del" select="'Delete!'"/>

<xsl:variable name="componentinfo.fundesc" select="'Function Description'"/>

<xsl:variable name="componentinfo.deven" select="'Development Environment Description'"/>

<xsl:variable name="componentinfo.comname" select="'Component Name'"/>

<xsl:variable name="componentinfo.alias" select="'Alias'"/>

<xsl:variable name="componentinfo.id" select="'Serial No'"/>

<xsl:variable name="componentinfo.version" select="'Version'"/>

<xsl:variable name="componentinfo.author" select="'Author(s)'"/>

<xsl:variable name="componentinfo.devlan" select="'Development Language(s)'"/>

<xsl:variable name="componentinfo.devplat" select="'Development Platform(s)'"/>

<xsl:variable name="componentinfo.status" select="'Status'"/>

<xsl:variable name="componentinfo.confirmd" select="'Confirm Date'"/>

<xsl:variable name="componentinfo.confirmp" select="'Confirm Person'"/>

<xsl:variable name="componentinfo.browse" select="'Browsed times'"/>

<xsl:variable name="componentinfo.security" select="'Security Setting'"/>

<xsl:variable name="componentinfo.dev" select="'developer(s)'"/>

<xsl:variable name="componentinfo.admin" select="'Administrator(s)'"/>

<xsl:variable name="componentinfo.ispub" select="'Is it public?'"/>

<xsl:variable name="componentinfo.deploysf" select="'Deploy source files'"/>

<xsl:variable name="componentinfo.deployws" select="'Deploy web service'"/>

<xsl:template match="col:ComponentBasicInfo">

<script language="Javascript" type="text/javascript" src="scripts/component.js">// JS code that handle events</script>

<noscript>This page need Javascript</noscript>

<table width="100%"  border="0">

  <tr>

    <td><h1><xsl:value-of  select="$componentinfo.cominfo"/></h1>

    <xsl:variable name="serialno" select="col:SerialNo"/>

    <table border="0" cellpadding="2" cellspacing="1">

                           <tbody>

                                  <tr>

                                         <td><input type="button" name="edit" value="{$template.edit}" onclick="ComponentEdit('{$serialno}')"/>_u47 ?td>

                                         <td><input type="button" name="edit" value="{$template.down}" onclick="ComponentDownload('{$serialno}')"/></td>

                                         <td><input type="button" name="edit" value="{$template.confirm}" onclick="ComponentConfirm('{$serialno}')"/></td>

                                         <td><input type="button" name="edit" value="{$template.del}" onclick="ComponentDelete('{$serialno}')"/></td>

                                  </tr>

                           </tbody>

                     </table>

       </td>

    <td></td>

  </tr>

  <tr>

    <td><table width="100%"  border="0">

              <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.comname"/></td>

                     <td><xsl:value-of  select="col:Name"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.alias"/>X/td>

                     <td><xsl:value-of  select="col:Alias"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.id"/></td>

                     <td><xsl:value-of  select="$serialno"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.version"/></td>

                     <td><xsl:value-of  select="col:Version"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.author"/></td>

                     <td><xsl:value-of  select="col:Authors"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.devlan"/></td>

                     <td><xsl:value-of  select="col:DevelopmentLanguage"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.devplat"/></td>

                     <td><xsl:value-of  select="col:DevelopmentPlatform"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.status"/></td>

                     <td><xsl:value-of  select="col:Status"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.confirmd"/></td>

                     <td><xsl:value-of  select="col:ConfirmDate"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.confirmp"/></td>

                     <td><xsl:value-of  select="col:ConfirmBy"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.browse"/></td>

                     <td><xsl:value-of  select="col:BrowseTimes"/></td>

      </tr>

      <tr>

                     <td><h3><xsl:value-of  select="$componentinfo.security"/></h3></td>

                     <td></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.dev"/></td>

                     <td><xsl:value-of  select="col:Developers"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.admin"/></td>

                     <td><xsl:value-of  select="col:Admins"/></td>

      </tr>

      <tr>

                     <td bgcolor="#E0F1FF"><xsl:value-of  select="$componentinfo.ispub"/></td>

                     <td><xsl:value-of  select="col:IsPublic"/></td>

      </tr>

      <tr>

                     <td><a href="dispatchcvs.do%serial={$serialno}"><xsl:value-of  select="$componentinfo.deploysf"/></a></td>

                     <td></td>

      </tr>

      <tr>

                     <td><a href="webservicepredeploy.do%serial={$serialno}"><xsl:value-of  select="$componentinfo.deployws"/></a></td>

                     <td></td>

      </tr>

    </table></td>

    <td  valign="top">

    <table width="100%"  border="0">

      <tr>

        <td  bgcolor="#E0F1FF"><h3><xsl:value-of  select="$componentinfo.fundesc"/></h3></td>

      </tr>

      <tr>

        <td><xsl:value-of  select="col:FunctionDescription"/></td>

      </tr>

      <tr>

        <td  bgcolor="#E0F1FF"><h3><xsl:value-of  select="$componentinfo.deven"/></h3> </td>

      </tr>

      <tr>

         <td><xsl:value-of  select="col:DevelopmentEnvironment"/></td>

        </tr>

    </table>

    </td>

  </tr>

</table>

</xsl:template>

</xsl:stylesheet>

用浏览器打开cbidebug.xml文件显示如下:

用XSLT文件在浏览器格式化显示XML文件

效果还可以吧。