2016-03-30 60 views
-1

需要XSI屬性我有以下模式讓XS和個XML:在XSD

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="translator"> 
     ... 
    </xs:element> 
</xs:schema> 

我如何定義以下所需的屬性,所以添加新的翻譯節點時,這些屬性也將被添加?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="Translator.xsd" 

如果我把它們放在XSD,這樣

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="translator"> 
     <xs:attribute name="xmlns:xsi" type="xs:string" default="http://www.w3.org/2001/XMLSchema-instance"/> 
     <xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string" default="Translator.xsd"/> 
    </xs:element> 
</xs:schema> 

下面的問題是由Xerces的報道

[Error] :678:114: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'. 
[Error] :678:114: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration. 
[Error] :679:117: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xsi:noNamespaceSchemaLocation' is not a valid value for 'NCName'. 
[Error] :679:117: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration. 

回答

1

警告第一:XML Schema規範禁止在聲明屬性XML Schema實例名稱空間和explicitly discourages attempts to alter its behavior

說了這麼多,你所得到的是錯誤的原因是該名稱屬性只提供當地化的名字在目標命名空間(或者在這種情況下,沒有命名空間)新元素的定義。

你可以在技術上做這樣的事情,通過引用的xsi:noNamespaceSchemaLocation屬性,這是在內置XML Schema實例名稱空間已經定義:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xs:element name="translator"> 
     <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
        <xs:attribute ref="xsi:noNamespaceSchemaLocation" default="Translator.xsd"/> 
       </xs:extension> 
      </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

但是,你不能改變它的定義,因爲這屬性是內置的並以特殊的方式處理,我不確定你可以對其行爲產生多大影響。

+0

我只想在創建新的XML文件時將它們從XSD轉移到XML。 – nucandrei

+0

在進行建議更改後,在Xerces日誌中顯示以下警告:678:89:src-resolve.4.2:解析組件'xsi:noNamespaceSchemaLocation'時出現錯誤。檢測到'xsi:noNamespaceSchemaLocation'位於命名空間'http://www.w3.org/2001/XMLSchema-instance'中,但該命名空間中的組件不能從模式文檔'null'中引用。如果這是不正確的命名空間,則可能需要更改'xsi:noNamespaceSchemaLocation'的前綴。如果這是正確的名稱空間,那麼應該將一個適當的'import'標籤添加到'null'中。 – nucandrei

+0

嗨nucandrei,你可以嘗試使用http://stackoverflow.com/questions/17094247/where-is-the-xsd-file-for-http-www-w3-org-2001-xmlschema-但請記住,這是所有未經審查的,未經推薦的領土和加工商可能不會像您預期的那樣行事。 –