1. 程式人生 > >eclipse匯入本地xsd檔案(新手慎入)

eclipse匯入本地xsd檔案(新手慎入)

警告:以下內容如果對xml schema不瞭解可能會感覺不適。

eclipse使用xsd檔案可以輔助編輯xml檔案。如果我們自定義了schema檔案,需要匯入到xml catalog才可以生效。

如,自定義了logback配置檔案的xsd,名為:logback.xsd

那麼匯入Eclipse中的方法有兩種。

匯入Namespace name key

Preferences -> XML -> xml Catalog;

點選 Add 按鈕,分別輸入如下內容:

Location:選擇專案或檔案系統中的xsd檔案。
Key type: Namespace name
Key: http://logback.qos.ch/logback.xsd

那麼logback.xml檔案如下設定:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</configuration>

schemaLocation中填寫前面指定的”Key”的字串。

匯入Schema Location key

使用名稱空間

見多了Spring中的配置檔案可能會發現,schemaLocation是如下的形式指定的:

xsi:schemaLocation="[namespace] [schemalocation]"

如果要使用這種方式,需要建立SchemaLocation。

Preferences -> XML -> xml Catalog;

點選 Add 按鈕,分別輸入如下內容:

Location:選擇專案或檔案系統中的xsd檔案。
Key type: Schema Location
Key: http://logback.qos.ch/logback.xsd

相應的配置檔案如下:

<configuration xmlns="http://logback.qos.ch/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://logback.qos.ch/logback http://logback.qos.ch/logback.xsd">

此處的名稱空間是schema中定義的targetNamespace,而不是當前xml檔案的xmlns。

不使用名稱空間

如果schema定義中沒有使用名稱空間,那麼使用noNamespaceSchemaLocation指定schema的key。

由於xsd中沒有使用名稱空間,那麼xml也不要使用名稱空間,即”xmlns”不要設定,否則無法在xmlns中找xsd定義的無名稱空間的configuration節點。

<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceschemaLocation="http://logback.qos.ch/logback0.xsd">

注:xsd檔案也需要刪除targetNameSpace。

如果eclipse提示:white space is required between publicid and systemid 可以在xml第二行新增:<!DOCTYPE configuration>,注意修改根節點名稱。

測試發現一個問題,可以進行語法校驗,但沒有語法提示,或許eclipse的xml editor是根據ns去檢索的?。

小結:

使用 “Namespace name”,在eclipse增加catalog為Namespace,那麼xml中直接使用名稱空間就可以使用xsd檔案來控制xml。

使用 “Schema location”,分為兩種情況:

規範的使用名稱空間,xml中需要通過屬性"xsi:schemaLocation"來指定"[namespace] [schemalocation]";

不使用名稱空間,xml中需要通過屬性"xsi:noNamespaceschemaLocation"來指定"Scheme location"。注:xsd和xml均不可使用名稱空間。

xsd檔案修改以後,需要在xml catalog中reload一下,xml檔案也關閉重新開啟一下。

例項

你希望為logback建立一個schema。

使用名稱空間的schema

schema程式碼

限於篇幅,只寫了appender節點。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://logback.qos.sh/logback"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:tns="http://logback.qos.sh/logback" elementFormDefault="qualified" attributeFormDefault="qualified">

    <element name="configuration" type="tns:ConfiurationType"></element>

    <complexType name="ConfiurationType">
        <sequence>
            <element name="appender" type="tns:AppenderType"></element>
        </sequence>
        <attribute name="debug" type="boolean" default="false"></attribute>
        <attributeGroup ref="tns:scanConf"></attributeGroup>
    </complexType>
    <attributeGroup name="scanConf">
        <attribute name="scan" type="boolean" default="false"></attribute>
        <attribute name="scanPeriod" type="string" default="60 seconds"></attribute>
    </attributeGroup>

    <complexType name="AppenderType">
        <sequence>
            <element name="Encoder" type="tns:EncoderType" maxOccurs="1" minOccurs="1"></element>
        </sequence>
        <attribute name="name" type="string" use="required"></attribute>
        <attribute name="class" type="string" use="required"></attribute>
    </complexType>

    <complexType name="EncoderType">
        <sequence>
            <element name="pattern" type="string" maxOccurs="1" minOccurs="1"></element>
        </sequence>
    </complexType>

</schema>

xml catalog中配置Namespace name

xml catalog配置項

Location:選擇這個xsd檔案
Key type: Namespace name
Key: http://logback.qos.sh/ns

注,這裡的key沒有強制要求必須是xsd中定義的targetNamespace,或者說這裡可以覆蓋預設的targetNamespace。

xml引用Namespace name
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</configuration>

xml中預設名稱空間,即xmlns與配置的Key相同,即可生效。

另外一種配置方式,我的xml中配置了自己的預設名稱空間,那麼可以為schema指定別名:

<?xml version="1.0" encoding="UTF-8"?>
<logback:configuration xmlns="http://www.example.com/myapp" xmlns:logback="http://logback.qos.sh/ns">
</logback:configuration>

這是xml規範的基本知識,更多參考xml規範文件。

xml catalog中配置schema location

xml catalog配置項

注,這裡的key沒有強制要求必須是xsd檔案的物理uri,但是,為了便於你的使用者深入瞭解細節,應該提供物理存在的uri。

xml引用Schema location
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd">
</configuration>

注:這裡xmlns為schema檔案中的targetNamespace。

同樣有另一種別名的方式:

<?xml version="1.0" encoding="UTF-8"?>
<logback:configuration xmlns="http://www.example.com/myapp" xmlns:logback="http://logback.qos.sh/logback"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logback.qos.sh/logback http://logback.qos.ch/logback.xsd">
</logback:configuration>

注:這裡 xmlns:logback 為schema檔案中的targetNamespace。

不使用名稱空間的schema

schema程式碼

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:tns="http://logback.qos.sh/logback" elementFormDefault="qualified">

    <xs:element name="configuration" type="ConfiurationType"></xs:element>

    <xs:complexType name="ConfiurationType">
        <xs:sequence>
            <xs:element name="appender" type="AppenderType"></xs:element>
        </xs:sequence>
        <xs:attribute name="debug" type="xs:boolean" default="false"></xs:attribute>
        <xs:attributeGroup ref="scanConf"></xs:attributeGroup>
    </xs:complexType>
    <xs:attributeGroup name="scanConf">
        <xs:attribute name="scan" type="xs:boolean" default="false"></xs:attribute>
        <xs:attribute name="scanPeriod" type="xs:string" default="60 seconds"></xs:attribute>
    </xs:attributeGroup>

    <xs:complexType name="AppenderType">
        <xs:sequence>
            <xs:element name="Encoder" type="EncoderType" maxOccurs="1" minOccurs="1"></xs:element>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required"></xs:attribute>
        <xs:attribute name="class" type="xs:string" use="required"></xs:attribute>
    </xs:complexType>

    <xs:complexType name="EncoderType">
        <xs:sequence>
            <xs:element name="pattern" type="xs:string" maxOccurs="1" minOccurs="1"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

與前面的比較就是刪除了targetNamespace,同時將型別引用中的tns:字首刪除。

xml catalog中配置schema location

xml catalog配置項

注,這裡的key沒有強制要求必須是xsd檔案的物理uri,但是,為了便於你的使用者深入瞭解細節,應該提供物理存在的uri。

xml引用Schema location
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://logback.qos.sh/logback_nons.xsd">
<appender name="d" class="c">
    <Encoder>
        <pattern>a</pattern>
    </Encoder>
</appender>
</configuration>

相關推薦

eclipse匯入本地xsd檔案(新手)

警告:以下內容如果對xml schema不瞭解可能會感覺不適。 eclipse使用xsd檔案可以輔助編輯xml檔案。如果我們自定義了schema檔案,需要匯入到xml catalog才可以生效。 如,自定義了logback配置檔案的xsd,名為:logbac

eclipse匯入本地的svn專案後不能在team提交更新

由於專案是在本地有svn檢出,然後再想通過eclipse 修改然後在eclipse內部提交和更新,但是此時,team裡並沒有update和commit選項, 又不想重新再檢出一次專案,怎麼辦? 可以在eclipse裡先安裝svn外掛,然後此時點選右鍵檢視team會有一個 share import選項,此時

eclipse匯入專案(檔案)解決方法

在Eclipse(myeclipse也相同)中匯入專案或檔案的時候,會遇到亂碼的問題,這裡整理一下網上搜到的三種方法 一. 設定工作空間的編碼 編輯器的編碼會影響到所有的專案中的字元的顯示,可以說是作用最為廣泛的設定,每一個專案都會受到這個設定的影響。 點選選單

php 爬蟲的簡單實現, 獲取整個頁面, 再把頁面的資料匯入本地檔案當中

$curlobj = curl_init(); //建立一個curl 的資源,下面要用的 curl_setopt($curlobj,CURLOPT_URL,"http://www.baidu.com

31.Elasticsearch批量匯入本地Json檔案Java實現(ES檔案同步)

題記產品開發需要,我們需要將網際網路採集的資料儲存到ES中,以實現資料的全文檢索。網際網路採集的資料,往往格式雜亂,需要先進行資料清洗操作。而ES支援的入庫格式,json格式資料會相對方便些。本文主要介紹,如何將格式化的Json檔案批量插入到ES中。1、需提前做的工作1)設計

pycharm匯入本地py檔案時,模組下方出現紅色波浪線時如何解決

有時候匯入本地模組或者py檔案時,下方會出現紅色的波浪線,但不影響程式的正常執行,但是在檢視源函式檔案時,會出現問題 問題如下: 解決方案: 1. 進入設定,找到Console下的Pyth

eclipse新增本地xsd

DTD型別約束檔案 :    1. Window->Preferences->XML->XML Catalog->UserSpecified Entries視窗中,選擇Add

關於spring mvc載入本地xsd檔案問題

windows>preferences>myeclipse>files and edtitors>xml>xmlcatalog  點add,在出現的視窗中的Key Type中選擇URI,在location中選File syetem,然後在spr

Elasticsearch批量匯入本地Json檔案Java實現

題記 產品開發需要,我們需要將網際網路採集的資料儲存到ES中,以實現資料的全文檢索。 網際網路採集的資料,往往格式雜亂,需要先進行資料清洗操作。 而ES支援的入庫格式,json格式資料會相對方便些。 本文主要介紹,如何將格式化的Json檔案批量插入到ES

IntelliJ IDEA 新增本地xsd檔案

1.開啟設定File-->Settings(或者Ctrl + Alt + S)--->Languages&Frameworks-->Schemas and DTDS 2.選擇右

向Navicat匯入本地sql檔案出現錯誤解決

資料庫真是一門學問,由於做java 還有javaweb作業需要用到資料庫,所以正用正學習吧,不過遇到的問題很多,就比如把從navicat匯出的sql檔案再重新匯入進去navicate中去,搞了好久總是不成功,總是出錯。 出現此問題的解決方法(最後竟然在百度

Eclipse匯入dtd和xsd檔案,使XML自動提示

DTD 型別約束檔案     1. Window->Preferences->XML->XML Catalog->User Specified Entries視窗中,選擇Add 按紐     2.在Add XML Catalog Entry 對話方塊中選擇或輸入以下內容:       

pycharm匯入本地檔案,程式執行正常,但匯入模組時出現紅色波浪線

pycharm匯入本地檔案,程式執行正常,但匯入模組時出現紅色波浪線,如下所示: 兩種解決辦法: (1)在檔案前面加“.” (2)點選選單欄的“PyCharm”, 然後選擇“Preferences”,接著依次執行以下操作,最後點選“OK”按鈕。 (3) 

Eclipse匯入Maven專案詳解(新手初學)

最近遇到Maven管理下的spring MVC專案,組內某位將專案程式碼扔過來,一臉懵逼(囧),查閱了一些資料後終於將此專案執行通了(>_<),特此記錄下來與各位分享。 通俗的來說,Maven就是個類似於Git的專案管理工具。而Spring MVC就是將M(Mo

eclipse匯入maven專案,pom檔案報錯解決辦法

Project build error: Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Failure to transfer org.springframework.boot:sprin

eclipse匯入class檔案

方法二:右鍵src資料夾-》build path-》config build path-》library-》add class folder-》create new folder,此時在工程中會出現Referenced Librarier,在其中包含了你的新資料夾,同時在其下面出現了你的新資料夾,這時右鍵你

eclipse匯入maven專案pom檔案報Missing artifact 。。。錯誤的解決辦法

今天 在eclipse裡面匯入一個以前執行無誤的maven專案居然發現pom 檔案報錯 提示Missing artifact 。。。。 的錯誤,很是苦惱,後來在百度上搜了各種方法 終於解決了問題 原因是 pom檔案有時候會犯蠢 你必須強制告訴他本地倉庫在哪裡

eclipse匯入約束檔案

DTD 型別約束檔案     1. Window->Preferences->XML->XML Catalog->User Specified Entries視窗中,選擇Add 按紐     2.在Add XML Catalog Entry 對話方塊中選擇或輸入以下內容:       

Eclipse直接開啟類檔案/資料夾所在的本地目錄

1.Eclipse原生的檔案瀏覽操作        選擇專案目錄/檔案 按 ALT+SHIFT +W ,      會彈出選單點選 System Explorer 就可以開啟檔案所在的本地目錄了;      個人覺得還是快捷鍵使用較為習慣。 2.設定工具目錄 Run -

eclipse匯入svn專案之後java檔案圖示空心問題的解決

eclipse匯入svn專案之後java檔案圖示空心並且檔案不能編輯, 問題是它還不是一個真正的java project或者maven project, 需要再次import maven project就可以了