2012-08-31 31 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元素将不在名称空间中。