2012-12-06 35 views
6

给定一个顶级xsd导入第二个级别,然后导入第三个级别,是否可以使用第一个级别的第三个级别?或者第一个必须直接导入第三个?从xsd使用类型从导入导入

回答

1

如果type是你在说些什么的话..这<xs:Include><xs:Import> ..

答案是:分析器需要连接所有的XSD一起照顾

见下面的例子:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <child>trial</child> 
    <child2>trial2</child2> 
    <trunk> 
    <branch>1</branch> 
    <branch>2</branch> 
    </trunk> 
    <specialchild>test</specialchild> 
</root> 

对于上述XML我将设计的XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="include multiple XSDs2.xsd"/> 
    <xs:element name="root" type ="root"/> 
    <xs:complexType name="root"> 
    <xs:sequence> 
     <xs:element name="child" type="xs:string" /> 
     <xs:element name="child2" type="xs:string" /> 
     <xs:element name="trunk" type="trunk"/> 
     <xs:element name="specialchild" type="specialchild"/> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

其中类型中继线在import multiple XSDs2.xsd文件中定义和链接通过使用<xs:include> ..(其驻留在相同的文件夹)。并且代码看起来是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="include multiple XSDs3.xsd"/> 
    <xs:complexType name="trunk"> 
    <xs:sequence> 
     <xs:element maxOccurs="unbounded" name="branch" type="branch" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

和类型分支是一个简单类型include multiple XSDs3.xsd文件定义,代码如下:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:simpleType name="branch"> 
    <xs:restriction base="xs:string"/> 
    </xs:simpleType> 
    <xs:simpleType name="specialchild"> 
    <xs:restriction base="xs:string"/> 
    </xs:simpleType> 
</xs:schema> 

* 现在棘手的问题是:specialchildXSD_1宣布作为地方在XSD_3中定义,这两个XSD通过XSD_2连接。 您可以观察到默认情况下解析器负责连接所有XSD并将它们全部视为一个! *

希望这能解决您的问题!

3

好问题! 从阅读规范,我不能告诉。它没有明确地解决传递导入的问题。

在底漆(部分0),他们只谈论进口的约一层:http://www.w3.org/TR/xmlschema-0/#import

在结构(部分1),它也只定义直接导入http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#composition-schemaImport既然谈到“解决这样的手段外部组件“(我认为这意味着命名空间),也许有必要假定一种明确的方式来处理导入的模式是必要的 - 换句话说,每个导入模式都需要一个明确的命名空间。

谷歌搜索显示,其他人也被这个问题困惑:

什么是最关心的那些帖子是不同的XSD的处理器有不同行为 - 这表明处理器的作者也感到困惑。

虽然自那些帖子(2002,2005)以来可能已经发生了变化,但最明智的做法似乎是避免这个问题,并且只使用直接导入,因为这将适用于所有处理器。

正如我所说:很好的问题。


下面是测试,来检查XSD处理器(当然,这并不能保证它会为别人工作,否则使用一些其他的XSD处理器...)。我发现我最喜欢的一个(xmllint)不允许允许传递导入。

测试是三个模式:a.xsd进口b.xsd这反过来进口c.xsd;和在定义的类型c.xsda.xsd引用:

<!-- a.xsd --> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.c.com" targetNamespace="http://www.a.com"> 
    <xsd:import namespace="http://www.b.com" schemaLocation="b.xsd"/> 
<!-- UNCOMMENT FOR DIRECT IMPORT 
    <xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/> 
--> 
    <xsd:element name="eg" type="c:TypeC"/> 
</xsd:schema> 

<!-- b.xsd --> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/> 
</xsd:schema> 

<!-- c.xsd --> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.c.com"> 
    <xsd:simpleType name="TypeC"> 
     <xsd:restriction base="xsd:string"/> 
    </xsd:simpleType> 
</xsd:schema> 


<!-- a.xml --> 
<eg xmlns="http://www.a.com">hello world</eg> 

$ xmllint --schema a.xsd a.xml --noout 
a.xsd:6: element element: Schemas parser error : Element 
'{http://www.w3.org/2001/XMLSchema}element', attribute 'type': References from 
this schema to components in the namespace 'http://www.c.com' are not allowed, 
since not indicated by an import statement. 
WXS schema a.xsd failed to compile 

的错误信息是:References from this schema to components in the namespace 'http://www.c.com' are not allowed, since not indicated by an import statement.,表明xmllint的开发者至少是相当肯定进口不可传递