Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何动态地将需求放置在<;xs:any/>;使用多个模式阻止?_Xml_Schema - Fatal编程技术网

Xml 如何动态地将需求放置在<;xs:any/>;使用多个模式阻止?

Xml 如何动态地将需求放置在<;xs:any/>;使用多个模式阻止?,xml,schema,Xml,Schema,考虑以下XML文档: <?xml version="1.0" encoding="utf-8"?> <fooDb version="1.0.0.0" xmlns="http://tempurl.org/2013/01/Example"> <provider>Some Provider</provider> <connectionSettings xmlns="http://tempurl.org/2013/01/Example.Som

考虑以下XML文档:

<?xml version="1.0" encoding="utf-8"?>
<fooDb version="1.0.0.0" xmlns="http://tempurl.org/2013/01/Example">
  <provider>Some Provider</provider>
  <connectionSettings xmlns="http://tempurl.org/2013/01/Example.SomeProvider">
    <protocol>https</protocol>
    <server>contoso-server</server>
    <port>8080</port>
    <project>Tailspin Toys</project>
  </connectionSettings>
</fooDb>

某些提供者
https
contoso服务器
8080
尾旋玩具
基本上,我有这个
元素,我希望客户端能够自定义它。我不想让整个“示例”项目知道特定提供者对其连接设置做了什么。我第一次尝试的架构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ExampleConfiguration"
    xmlns="http://tempurl.org/2013/01/Example"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://tempurl.org/2013/01/Example"
    elementFormDefault="qualified">

  <!-- Root element for configurations. -->
  <xs:element name="fooDb">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="provider" minOccurs="1" maxOccurs="1" />
        <xs:element ref="connectionSettings" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
      <xs:attribute name="version" type="version" />
    </xs:complexType>
  </xs:element>

  <xs:element name="provider" type="xs:string" />

  <!-- Individual providers provide their own connection settings. -->
  <xs:element name="connectionSettings">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="strict" minOccurs="0" maxOccurs="unbounded"
                namespace="##other" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- Requires that an element be empty. -->
  <xs:simpleType name="empty">
    <xs:restriction base="xs:string">
      <xs:enumeration value=""/>
    </xs:restriction>
  </xs:simpleType>

  <!-- Current version is 1.0.0.0. -->
  <xs:simpleType name="version">
    <xs:restriction base="xs:string">
      <xs:enumeration value="1.0.0.0"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

一个特定的提供者,我们称之为“SomeProvider”,它定义自己的连接设置:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SomeProviderConfiguration"
    xmlns="http://tempurl.org/2013/01/Example.SomeProvider"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://tempurl.org/2013/01/Example.SomeProvider"
    elementFormDefault="qualified">

  <xs:element name="connectionSettings">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="protocol" type="protocol" />
        <xs:element name="server" type="xs:string" />
        <xs:element name="port" type="port" />
        <xs:element name="project" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="protocol">
    <xs:restriction base="xs:string">
      <xs:enumeration value="http" />
      <xs:enumeration value="https" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="port">
    <xs:restriction base="xs:int">
      <xs:minInclusive value="0" />
      <xs:maxInclusive value="65535" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

当我尝试验证它时,我得到了一个失败,因为外部模式期望
节点被定义为“Example”名称空间的一部分,但它实际上属于“Example.SomeProvider”名称空间(至少正如我上面定义的那样)


这样的事情在模式中是可以以这种方式表达的,还是我必须求助于模式包含方案?

如果我使用您提供的示例XML,您不能在“示例”名称空间中定义“connectionSettings”。你的食物看起来像

<xs:element name="fooDb">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="provider" minOccurs="1" maxOccurs="1" />
        <xs:any processContents="strict" minOccurs="1" maxOccurs="1"
                namespace="##other" />
      </xs:sequence>
    </xs:complexType>
</xs:element>

但我猜您正在尝试强制提供程序重写“connectionSettings”,而不与“Example”共享相同的命名空间。我认为这是不可能的

如果您真的想确保“connectionSettings”是“Example”名称空间的一部分,那么提供者需要调整其XSD,如下所示

    <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SomeProviderConfiguration"
    xmlns="http://tempurl.org/2013/01/Example.SomeProvider"
    xmlns:par="http://tempurl.org/2013/01/Example"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://tempurl.org/2013/01/Example.SomeProvider"
    elementFormDefault="qualified">

        <xs:element name="protocol" type="protocol" />
        <xs:element name="server" type="xs:string" />
        <xs:element name="port" type="port" />
        <xs:element name="project" type="xs:string" />

  <xs:simpleType name="protocol">
    <xs:restriction base="xs:string">
      <xs:enumeration value="http" />
      <xs:enumeration value="https" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="port">
    <xs:restriction base="xs:int">
      <xs:minInclusive value="0" />
      <xs:maxInclusive value="65535" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<p:fooDb xmlns:p="http://tempurl.org/2013/01/Example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="..."
    xmlns:inc="http://tempurl.org/2013/01/Example.SomeProvider">
  <p:provider>p:provider</p:provider>
    <p:connectionSettings>
      <protocol xsi:type="inc:protocol" xmlns="http://tempurl.org/2013/01/Example.SomeProvider">https</protocol>
      <server xmlns="http://tempurl.org/2013/01/Example.SomeProvider">localhost</server>
      <port xsi:type="inc:port" xmlns="http://tempurl.org/2013/01/Example.SomeProvider">8001</port>
      <project xmlns="http://tempurl.org/2013/01/Example.SomeProvider">example</project>
    </p:connectionSettings>
</p:fooDb>

或者XML实例必须如下所示

    <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SomeProviderConfiguration"
    xmlns="http://tempurl.org/2013/01/Example.SomeProvider"
    xmlns:par="http://tempurl.org/2013/01/Example"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://tempurl.org/2013/01/Example.SomeProvider"
    elementFormDefault="qualified">

        <xs:element name="protocol" type="protocol" />
        <xs:element name="server" type="xs:string" />
        <xs:element name="port" type="port" />
        <xs:element name="project" type="xs:string" />

  <xs:simpleType name="protocol">
    <xs:restriction base="xs:string">
      <xs:enumeration value="http" />
      <xs:enumeration value="https" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="port">
    <xs:restriction base="xs:int">
      <xs:minInclusive value="0" />
      <xs:maxInclusive value="65535" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<p:fooDb xmlns:p="http://tempurl.org/2013/01/Example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="..."
    xmlns:inc="http://tempurl.org/2013/01/Example.SomeProvider">
  <p:provider>p:provider</p:provider>
    <p:connectionSettings>
      <protocol xsi:type="inc:protocol" xmlns="http://tempurl.org/2013/01/Example.SomeProvider">https</protocol>
      <server xmlns="http://tempurl.org/2013/01/Example.SomeProvider">localhost</server>
      <port xsi:type="inc:port" xmlns="http://tempurl.org/2013/01/Example.SomeProvider">8001</port>
      <project xmlns="http://tempurl.org/2013/01/Example.SomeProvider">example</project>
    </p:connectionSettings>
</p:fooDb>

p:提供者
https
本地服务器
8001
例子