Xsd模式名称空间

本文关键字:空间 模式 Xsd | 更新日期: 2023-09-27 17:50:42

请考虑下面的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。验证类抛出

命名空间中的元素'PspShoppingCartServiceRequest''http://www.example.com'有无效的子元素命名空间中的'PspShoppingCartServiceRequestBody'"http://www.example.com"。预期的可能元素列表:命名空间'http://www.example.com'中的'PspRequestHeader' .

应该是"http://www.example.com/common"吗?

Xsd模式名称空间

不能,因为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" />