Xsd 架构命名空间

Xsd Schema Namespace

请考虑以下PspShoppingCartServiceRequest.Xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns:tns="http://www.example.com"
    elementFormDefault="qualified"
    targetNamespace="http://www.example.com"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:common="http://www.example.com/common"    
    <xs:import namespace= "http://www.example.com/common" schemaLocation="common.xsd" />
    <xs:element name="PspShoppingCartServiceRequest" type="tns:PspShoppingCartServiceRequest" />
    <xs:complexType name="PspShoppingCartServiceRequest">
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="PspRequestHeader" type="common:PspRequestHeader" />
            <xs:element minOccurs="1" maxOccurs="1" name="PspShoppingCartServiceRequestBody" type="tns:PspShoppingCartServiceRequestBody" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="PspShoppingCartServiceRequestBody">
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="PspShoppingCart" type="tns:PspShoppingCart" />
            <xs:element minOccurs="1" maxOccurs="1" name="OrderId" type="common:OrderIdType" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Common.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
  targetNamespace="http://www.example.com/common"
  elementFormDefault="qualified"
  xmlns:common="http://www.example.com/common"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="PspRequestHeader">
    <xs:all>
      <xs:element minOccurs="1" maxOccurs="1" name="MerchantId" type="common:MerchantIdType" />
      <xs:element minOccurs="1" maxOccurs="1" name="RequestDatetime" type="common:RequestDateTimeType" />
      <xs:element minOccurs="1" maxOccurs="1" name="RequestReferenceNumber" type="common:RequestReferenceNumberType" />
      <xs:element minOccurs="1" maxOccurs="1" name="Language" type="common:PspLanguageType" />
    </xs:all>
  </xs:complexType>
</xs:schema>

我知道 xsds 上有缺失的部分。我没有写所有的元素,因为我们不必看那些部分

我希望我的 PspRequestHeader 必须在命名空间“http://www.example.com/common”中,但是当我尝试验证即将到来的 xml 时,如果它不包含 "PspRequestHeader" 元素,XDocument.Validate class 抛出

The element 'PspShoppingCartServiceRequest' in namespace 'http://www.example.com' has invalid child element 'PspShoppingCartServiceRequestBody' in namespace 'http://www.example.com'. List of possible elements expected: 'PspRequestHeader' in namespace 'http://www.example.com'.

不应该是例子“http://www.example.com/common”吗?

不,因为 targetNamespace 是 http://www.example.com

如果缺少输入 xml PspRequestHeader,则消息将显示如下内容。根据声明,它是必需的,因为它有 minOccurs=1maxOccurs=1.

这是因为该元素是局部元素声明,将元素的类型指定为common:PspRequestHeader。您应该在 Common.xsd:

中声明一个全局元素
<xs:element name="PspRequestHeader" type="common:PspRequestHeaderType" />
<xs:complexType name="PspRequestHeaderType">
  <xs:all>
    <xs:element minOccurs="1" maxOccurs="1" name="MerchantId" type="common:MerchantIdType" />
    <xs:element minOccurs="1" maxOccurs="1" name="RequestDatetime" type="common:RequestDateTimeType" />
    <xs:element minOccurs="1" maxOccurs="1" name="RequestReferenceNumber" type="common:RequestReferenceNumberType" />
    <xs:element minOccurs="1" maxOccurs="1" name="Language" type="common:PspLanguageType" />
  </xs:all>
</xs:complexType>

并在 PspShoppingCartServiceRequest 中使用 ref 属性引用它:

<xs:element minOccurs="1" maxOccurs="1" ref="common:PspRequestHeader" />

像这样更改架构位置并尝试:

<xs:import namespace= "http://www.example.com/common" schemaLocation="../common.xsd" />