我可以创建一个xml来指定来自2个嵌套xsd的元素而不使用前缀吗?

时间:2012-06-29 08:28:02

标签: xml visual-studio xslt xml-validation

我有两个嵌套的xsd:

DefaultSchema.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DefaultSchema"
    targetNamespace="http://myNamespace.com/DefaultSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://myNamespace.com/DefaultSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="ZForm">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Part" minOccurs="0" maxOccurs="unbounded" type="Part"/>
    </xs:sequence>
    <xs:attribute name="Title" use="required" type="xs:string"/>
    <xs:attribute name="Version" type="xs:int"/>
  </xs:complexType>

  <xs:complexType name="Part">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Label" type="Label" minOccurs="0"></xs:element>
    </xs:sequence>
    <xs:attribute name="Title" use="required" type="xs:string"/>
  </xs:complexType>
  <xs:complexType name="Label">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Title" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

ExportSchema.xsd:(这个有点在DefaultSchema的主要元素(ZForm)周围包含1个元素(ZForms))

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ExportSchema"
    targetNamespace="http://myNamespace.com/ExportSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://myNamespace.com/DefaultSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:es="http://myNamespace.com/ExportSchema.xsd"
>
  <xs:import namespace="http://myNamespace.com/DefaultSchema.xsd" schemaLocation="DefaultSchema.xsd"/>

  <xs:element name="ZForms" type="es:ZFormType"></xs:element>

  <xs:complexType name="ZFormType">
    <xs:sequence>
      <xs:element name="ZForm" type="ZForm" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

最后我有一个生成的xml

<?xml version="1.0" encoding="utf-8"?>
<ZForms xmlns="http://myNamespace.com/ExportSchema.xsd">
  <ZForm Version="1" Title="FormTitle">
    <Part Title="PartTitle" >
      <Label Title="LabelTitle" />
    </Part>
  </ZForm>
</ZForms>

Visual Studio抱怨它不知道'Part'是什么 我希望我不需要使用xml名称空间前缀(..)来进行此xml验证,因为ExportSchema.xsd具有对DefaultSChema.xsd的引用。

有没有办法在不明确指定DefaultSchema.xsd的情况下使xml结构有效?或者这不是吗?

2 个答案:

答案 0 :(得分:1)

如果您从基础架构的import更改为include,则可以使其工作(没有名称空间前缀):

<强> ExportSchema.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ExportSchema"
    targetNamespace="http://myNamespace.com/DefaultSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://myNamespace.com/DefaultSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:include schemaLocation="DefaultSchema.xsd" />

    <xs:element name="ZForms" type="ZFormType"></xs:element>

    <xs:complexType name="ZFormType">
        <xs:sequence>
            <xs:element name="ZForm" type="ZForm" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

注意:这还需要将目标命名空间更改为DefaultSchema.xsd。

From MSDN on xsd:import

include 元素和 import 元素之间的区别在于import元素允许从具有不同目标名称空间的模式文档引用模式组件,include元素添加来自其他模式组件的模式组件与包含架构具有相同目标命名空间(或没有指定目标命名空间)的架构文档。简而言之,import元素允许您使用任何模式中的模式组件; include元素允许您将包含的模式的所有组件添加到包含模式。

DefaultSchema.xsd (对您的版本没有变化)

<强>的test.xml

<?xml version="1.0" encoding="utf-8"?>
<ZForms 
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://myNamespace.com/DefaultSchema.xsd ExportSchema.xsd" 
    xmlns="http://myNamespace.com/DefaultSchema.xsd">
  <ZForm Version="1" Title="FormTitle">    
    <Part Title="PartTitle" >      
      <Label Title="LabelTitle" />      
    </Part>
  </ZForm>
</ZForms>

这种组合似乎有效..

答案 1 :(得分:0)

您的架构文档DefaultSchema具有targetNamespace =“http://myNamespace.com/DefaultSchema.xsd”,因此它表示ZForm,Part和Label元素必须位于此命名空间中。但在您的实例中,这些元素不在此命名空间中。因此,您的实例对此架构无效。