Xsd架构命名空间

时间:2015-05-25 10:02:16

标签: c# xml xsd namespaces

请考虑以下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类将抛出

  

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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