2013-06-11 58 views
0

为了简单起见,我想在同一个命名空间中有一组文件,因为它们在概念上是相关的。我有一个主要或中心的xsd,将包含其他模式文件,基本上作为全局根元素。我的问题是最好的例子说明,但我基本上不能让我的非中心架构验证,这是一个命名空间的问题:如何创建同名命名空间设计一致的XSD?

模式1(支持):

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.company.org" 
      xmlns="http://www.person.org" 
      elementFormDefault="qualified"> 

    <xsd:simpleType name="test"> 
     <xsd:restriction base="xsd:string"> 
     </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:complexType name="PersonType"> 
     <xsd:sequence> 
      <xsd:element name="Name" type="test" /> 
      <xsd:element name="SSN" type="xsd:string" /> 
     </xsd:sequence> 
    </xsd:complexType> 

</xsd:schema> 

模式2(中心):

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.company.org" 
      xmlns="http://www.company.org" 
      elementFormDefault="qualified"> 

    <xsd:include schemaLocation="http://www.person.org"/> 

    <xsd:element name="Company"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="Person" type="PersonType" 
          maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

模式2很好,模式1不验证。 “test”没有命名空间,我不知道如何在不破坏我的意图的情况下为我的所有文件使用1个命名空间。

回答

0

目前还不清楚您要问什么问题,所以我猜想问题是“为什么架构文档1无法验证,何时架构文档2确认?”

我无法回答,因为我无法重现您的结果。这两个模式文档都会在您提供的表单中引发错误。

Schema文档1是指,在元件(http://www.company.org,名称),局部给复合型(http://www.company.org,PersonType)的定义,来命名类型(http://www.person.org,检验)。但名称空间http://www.person.org尚未导入,因此对该名称空间中组件的引用不合法。

规范type="test"被解释为参考(http://www.person.org,test),因为当“test”被解释为QName时,其名称空间名称将被视为默认名称空间(如果有)。这里,默认名称空间(在xsd:schema元素上声明)是http://www.person.org

如果 - 这是我的错误猜测 - 您想引用名称为(http://www.company.org,test)的类型,该类型在架构文档1的第7-10行中声明,那么您需要绑定命名空间前缀名称空间http://www.company.org并使用该前缀。它会工作,例如,(使用默认的命名空间,以避免不必考虑前缀的)名称的声明更改为

<xsd:element name="Name" type="tns:test" 
      xmlns:tns="http://www.company.org"/> 

或:

<xsd:element name="Name" type="test" 
      xmlns="http://www.company.org"/> 

注意,第7-10行声明的简单类型具有扩展名(http://www.company.org,test) - 我不知道你说''test'没有命名空间“是什么意思,但是你可能想检查你的假设。

架构文档2产生一个错误,因为您在第6行的xsd:include中指定的架构位置在取消引用时会生成非XSD架构文档(它是HTML页面)的文档。

我希望这会有所帮助。