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 具有不同子元素的同名XSD嵌套元素_Xml_Xsd_Complextype - Fatal编程技术网

Xml 具有不同子元素的同名XSD嵌套元素

Xml 具有不同子元素的同名XSD嵌套元素,xml,xsd,complextype,Xml,Xsd,Complextype,我目前正在重新编写一个XSD,以便在接受数据时更加具体。有一个complexType接受名为“part”且包含属性“partname”的所有元素,而不验证零件中的任何子节点。以下是当前complexType XSD代码: <xs:complexType name="partType" mixed="true"> <xs:sequence> <xs:any maxOccurs="unbounded" processContents="lax"

我目前正在重新编写一个XSD,以便在接受数据时更加具体。有一个complexType接受名为“part”且包含属性“partname”的所有元素,而不验证零件中的任何子节点。以下是当前complexType XSD代码:

<xs:complexType name="partType" mixed="true">
    <xs:sequence>
        <xs:any maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:attribute name="partname" type="stringtype" use="required" />
</xs:complexType>

下面是我收到的XML格式的“部件”类型示例:

<part partname="customer">
    <seq>1</seq>
    <type>owner</type>
    <part partname="conviction">
        <convictionCode>12345</convictionCode>
        <convictionDate>2011-06-02</convictionDate>
        <fine />
        <term />
        <type />
        <age>22</age>
        <suspension>0</suspension>
    </part>
    <part partname="claim">
        <dateOfLoss>2010-05-05</dateOfLoss>
        <claimType>Accident</claimType>
        <claimCost />
        <claimFault>No</claimFault>
        <type>owner</type>
    </part>
</part>

1.
主人
12345
2011-06-02
22
0
2010-05-05
意外事故
不
主人
当同一个元素以多种形式出现并且可以相互嵌套时,我很难找到有关如何实现内容验证的信息

任何关于什么样的语法可能合适的建议都会受到欢迎

当同一个元素以多种形式出现并且可以相互嵌套时,我很难找到有关如何实现内容验证的信息

通常,当父元素可以具有与父元素同名的子元素(或更一般的子元素)时,以下两种情况之一适用:

  • 父元素和子元素具有相同的类型,在结构上是递归的

    在这种情况下,关键要求是确保递归可以终止,方法是确保类似命名子级的出现是可选的。在这里,使元素或其类型或两者都成为顶级模式组件也很重要

    例如

    
    
    XSD将验证XML,但不会检查嵌套的
    部分
    元素;嵌套零件元素将属于未经检查的领域
    xs:any

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="part" type="partType"/>
      <xs:complexType name="partType">
        <xs:sequence>
          <xs:any maxOccurs="unbounded" processContents="lax" />
        </xs:sequence>
        <xs:attribute name="partname" use="required" />
      </xs:complexType>
    </xs:schema>
    
    然而,问题是,上面的XSD违反了规则,因为对于是否应该根据
    xs:any
    xs:element/@name=“part”
    验证
    part
    元素,存在歧义

    如果可以对XML进行结构更改,使包含元素的固定
    部分位于可变元素之前,则可以绕过唯一的粒子属性规则,仍然保留递归结构和
    xs:any
    允许的可变元素内容:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="part" type="partType"/>
      <xs:complexType name="partType">
        <xs:sequence>
          <xs:any maxOccurs="unbounded" processContents="lax" />
        </xs:sequence>
        <xs:attribute name="partname" use="required" />
      </xs:complexType>
    </xs:schema>
    
    XML
    
    12345
    2011-06-02
    22
    0
    2010-05-05
    意外事故
    不
    主人
    1.
    主人
    
    XSD 此XSD将成功验证上述XML:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="part" type="partType"/>
      <xs:complexType name="partType">
        <xs:sequence>
          <xs:element name="parts">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" 
                            name="part" type="partType"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:any maxOccurs="unbounded" processContents="lax" />
        </xs:sequence>
        <xs:attribute name="partname" use="required" />
      </xs:complexType>
    </xs:schema>
    

    嗨,kjhughes,谢谢你的意见。不幸的是,更改XML结构超出了范围,因为我们已经从多个第三方以及我们的内部系统收到了此XML。如果不是这样的话,我会同意改变结构是一条出路。再次感谢!
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="part" type="partType"/>
      <xs:complexType name="partType">
        <xs:sequence>
          <xs:any maxOccurs="unbounded" processContents="lax" />
          <xs:element maxOccurs="unbounded" name="part" type="partType"/>
        </xs:sequence>
        <xs:attribute name="partname" use="required" />
      </xs:complexType>
    </xs:schema>
    
    <?xml version="1.0"?>
    <part partname="customer">
        <parts>
          <part partname="conviction">
            <parts/>
            <convictionCode>12345</convictionCode>
            <convictionDate>2011-06-02</convictionDate>
            <fine />
            <term />
            <type />
            <age>22</age>
            <suspension>0</suspension>
          </part>
          <part partname="claim">
            <parts/>
            <dateOfLoss>2010-05-05</dateOfLoss>
            <claimType>Accident</claimType>
            <claimCost />
            <claimFault>No</claimFault>
            <type>owner</type>
          </part>
        </parts>
        <seq>1</seq>
        <type>owner</type>
    </part>
    
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="part" type="partType"/>
      <xs:complexType name="partType">
        <xs:sequence>
          <xs:element name="parts">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" 
                            name="part" type="partType"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:any maxOccurs="unbounded" processContents="lax" />
        </xs:sequence>
        <xs:attribute name="partname" use="required" />
      </xs:complexType>
    </xs:schema>