2014-10-03 158 views
1

嗨,我有一些XML对此我试图用我创建了一个模式来使用,但是当我试图解析XML,我得到以下错误信息:XML .XSD无效元素

The element 'PartnerPSTNTransfer' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0' has invalid child element 'InstallationAddress' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0'. List of possible elements expected: 'Configuration' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0'. 

下面是我的XML,我已经创建和与之相关的模式:

XML:

<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
         xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
         xmlns:a="http://localhost/Orders-Address-v1-0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:c="http://localhost/Orders-Connection-v1-0"> 
    <p:TelephoneNumber>01224507392</p:TelephoneNumber> 
    <p:StartDate>2014-10-25</p:StartDate> 
    <p:Postcode>co27pe</p:Postcode> 
    <p:InstallationAddress> 
    <a:NameNumber>10</a:NameNumber> 
    <a:Line1>Somewhere Road</a:Line1> 
    <a:City>Somewhere City</a:City> 
    <a:County>Somewhere County</a:County> 
    <a:Postcode>co2 7pe</a:Postcode> 
    </p:InstallationAddress> 
    <p:Configuration> 
    <padsl:Package>Data Only</padsl:Package> 
    <padsl:Feature>F0 F1 F2</padsl:Feature> 
    <padsl:Contract>Monthly_12</padsl:Contract> 
    </p:Configuration> 
</p:PartnerPSTNTransfer> 

PartnerPSTNTransfer的xsd:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="PartnerPSTNTransfer" 
    targetNamespace="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    elementFormDefault="qualified" 
    xmlns="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    xmlns:mstns="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://localhost/Orders-Address-v1-0" 
    xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:c="http://localhost/Orders-Common-v1-0" 
    xmlns:conn="http://localhost/Orders-Connection-v1-0"> 

    <xs:import namespace="http://localhost/Orders-Common-v1-0" schemaLocation="../../Common.xsd" /> 
    <xs:import namespace="http://localhost/Orders-Address-v1-0" schemaLocation="../../Address.xsd" /> 
    <xs:import namespace="http://localhost/Orders-PartnerPSTN-v1.0" schemaLocation="PartnerPSTN.xsd" /> 
    <xs:import namespace="http://localhost/Orders-Connection-v1-0" schemaLocation="../Connection.xsd" /> 

    <xs:complexType name="PartnerPSTNTransfer"> 
    <xs:sequence> 
     <xs:element name="TelephoneNumber" type="c:Landline" /> 
     <xs:element name="StartDate" type="xs:date" /> 
     <xs:element name="Postcode" type="c:RequiredString" /> 
     <xs:element name="InstallationAddress" type="a:Address" /> 
     <xs:element name="Configuration" type="padsl:PartnerPSTNConfiguration" /> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="PartnerPSTNTransfer" type="PartnerPSTNTransfer"></xs:element> 
</xs:schema> 

PSTNTransfer的xsd:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="PartnerPSTN" 
    targetNamespace="http://localhost/Orders-PartnerPSTN-v1.0" 
    elementFormDefault="qualified" 
    xmlns="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:mstns="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://localhost/Orders-Address-v1-0" 
    xmlns:c="http://localhost/Orders-Common-v1-0" > 

    <xs:import namespace="http://localhost/Orders-Common-v1-0" schemaLocation="../../Common.xsd"/> 

    <xs:simpleType name="Contract"> 
    <xs:restriction base="xs:token"> 
     <xs:enumeration value="Monthly_12"></xs:enumeration> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="PackageOption"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Data Only" /> 
     <xs:enumeration value="Evening and Weekend" /> 
     <xs:enumeration value="1000 Anytime" /> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="FeatureOption"> 
    <xs:list> 
     <xs:simpleType> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="F0"/> 
      <xs:enumeration value="F0A"/> 
      <xs:enumeration value="F0B"/> 
      <xs:enumeration value="F0C"/> 
      <xs:enumeration value="F1"/> 
      <xs:enumeration value="F2"/> 
      <xs:enumeration value="F2A"/> 
      <xs:enumeration value="F3"/> 
      <xs:enumeration value="F3A"/> 
      <xs:enumeration value="F3B"/> 
      <xs:enumeration value="F3C"/> 
     </xs:restriction> 
     </xs:simpleType> 
    </xs:list> 
    </xs:simpleType> 

    <xs:complexType name="PartnerPSTNConfiguration"> 
    <xs:sequence> 
     <xs:element name="Package" type="PackageOption" /> 
     <xs:element name="Feature" type="FeatureOption" /> 
     <xs:element name="Contract" type="Contract" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

我在正确的地方声明之前的配置元素,从而感到困惑,为什么它会抛出这个错误的InstallationAddress元素。

回答

1

使用xsi:schemaLocation暗示的XML处理器,对于http://localhost/Orders-PartnerPSTNTransfer-v1-0命名空间的XSD是PartnerPSTNTransfer.xsd

<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
         xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
         xmlns:a="http://localhost/Orders-Address-v1-0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:c="http://localhost/Orders-Connection-v1-0" 
         xsi:schemaLocation="http://localhost/Orders-PartnerPSTNTransfer-v1-0 
              PartnerPSTNTransfer.xsd"> 

进行上述更改后,并掘根出来的XSD悬空引用不包含在你的问题 ,我可以验证你的XML文件。也许您的XSD的旧版本正在从硬盘驱动器的意外位置获取。小心使用外部XML目录或其他工具机制来将XML文档实例与XSD关联。