2012-08-31 28 views
0

在我的模式文件中,我有一個元素Stat,它擴展了Entity。據我所知,I follow w3's example,但是當我去通過解析Java的DocumentBuilderFactorySchemaFactory模式(和使用該模式的XML)與我得到這個異常:如何正確地在模式中聲明擴展?

org.xml.sax.SAXParseException; systemId: file:/schema/characterschema.xsd; 
    src-resolve.4.2: Error resolving component 'cg:Entity'. It was detected that 
    'cg:Entity' is in namespace 'http://www.schemas.theliraeffect.com/chargen/entity', 
    but components from this namespace are not referenceable from schema document 
    'file:/home/andrew/QuasiWorkspace/CharacterGenerator/./schema/characterschema.xsd'. 
    If this is the incorrect namespace, perhaps the prefix of 'cg:Entity' needs 
    to be changed. If this is the correct namespace, then an appropriate 'import' 
    tag should be added to '/schema/characterschema.xsd'. 

所以,看來我在我的模式中看不到我的命名空間。我是否需要將自己的模式導入到自身中,還是完全誤讀了這個異常?難道我不正確地宣佈我的模式?

這是我參考架構:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity" 
    elementFormDefault="qualified"> 

    <xs:element name="Entity"> 
     <xs:complexType> 
      <xs:attribute name="id" type="xs:string" use="required"/>      
      <xs:attribute name="name" type="xs:string"/> 
      <xs:attribute name="description" type="xs:string"/> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="Stat"> 
     <xs:complexType> 
      <xs:complexContent> 
       <xs:extension base="cg:Entity"> 
        <xs:sequence> 
         <xs:element name="table" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>       
        </xs:sequence> 
       </xs:extension> 
      </xs:complexContent> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

回答

1

有兩個問題與你的架構。首先,您不聲明targetNamespace,因此您正在定義的EntityStat元素不在名稱空間中。其次,一個類型不能擴展一個元素,只能是另一個類型。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity" 
    targetNamespace="http://www.schemas.theliraeffect.com/chargen/entity" 
    elementFormDefault="qualified"> 

    <xs:complexType name="EntityType"> 
     <xs:attribute name="id" type="xs:string" use="required"/>      
     <xs:attribute name="name" type="xs:string"/> 
     <xs:attribute name="description" type="xs:string"/> 
    </xs:complexType> 

    <xs:element name="Entity" type="cg:EntityType" /> 


    <xs:complexType name="StatType"> 
     <xs:complexContent> 
      <xs:extension base="cg:EntityType"> 
       <xs:sequence> 
        <xs:element name="table" type="xs:string" minOccurs="0" 
          maxOccurs="unbounded"/>       
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:element name="Stat" type="cg:StatType" /> 
</xs:schema> 

在這裏,我限定兩個類型,其中之一延伸,另外,和各類型的兩個頂層元素。所有頂級的類型和元素定義爲架構的targetNamespace了,裏面StatType嵌套table元素也是這個命名空間中,因爲elementFormDefault="qualified"的 - 沒有這個EntityStat元素將在http://www.schemas.theliraeffect.com/chargen/entity命名空間,但在table元素將不在名稱空間中。