xml – 我可以重新定义XSD架构以更改类型子节点的maxOccurs属性吗?

前端之家收集整理的这篇文章主要介绍了xml – 我可以重新定义XSD架构以更改类型子节点的maxOccurs属性吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试扩展 XML模式以更改子元素在元素下发生的次数.

在原始模式中,父类型定义如下:

<xsd:complexType name="CrimeLineBusiness_Type">
    <xsd:complexContent>
        <xsd:extension base="PCLINEBUSINESS">
            <xsd:sequence>
                <xsd:element ref="CrimeSchedule" minOccurs="0"/>
                <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

我想这样做,所以CrimeSchedule和CrimeMoneyAndSecurities都是重复元素.我认为这可能有用(在xsd:redefine中):

<xsd:complexType name="CrimeLineBusiness_Type">
        <xsd:complexContent>
            <xsd:extension base="CrimeLineBusiness_Type">
                <xsd:sequence>
                    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0" maxOccurs="unbounded"/>
                    <xsd:element ref="CrimeSchedule" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

但是,我收到以下错误

cos-nonambig: CrimeSchedule and CrimeSchedule (or elements from their substitution group) violate “Unique Particle Attribution”.
During validation against this schema,ambiguity would be created for those two particles.

这是有道理的,因为这些孩子确实是在原始模式中定义的.那么,我怎样才能完成我需要做的事情呢?

谢谢!

进行扩展时,新类型的内容模型是基础内容模型的序列以及您在新类型中添加内容.例如,在您的情况下,重新定义的CrimeLineBusiness_Type类型的内容模型是
<xsd:sequence>
  <xsd:sequence>
    <xsd:element ref="CrimeSchedule" minOccurs="0"/>
    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
  </xsd:sequence>
  <xsd:sequence>
    <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element ref="CrimeSchedule" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:sequence>

您可以尝试实现您想要的是对该类型的双重重新定义.首先,您通过限制重新定义它,删除可选的CrimeSchedule和CrimeMoneyAndSecurities元素,然后通过扩展再次添加具有所需出现次数的元素.请参阅下面的完整工作示例:

text.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="test" type="CrimeLineBusiness_Type"/>

  <xsd:complexType name="PCLINEBUSINESS"/>
  <xsd:element name="CrimeSchedule"/>
  <xsd:element name="CrimeMoneyAndSecurities"/>

  <xsd:complexType name="CrimeLineBusiness_Type">
    <xsd:complexContent>
      <xsd:extension base="PCLINEBUSINESS">
        <xsd:sequence>
          <xsd:element ref="CrimeSchedule" minOccurs="0"/>
          <xsd:element ref="CrimeMoneyAndSecurities" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

</xsd:schema>

redef1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="test.xsd">

    <xs:complexType name="CrimeLineBusiness_Type">
      <xs:complexContent>
        <xs:restriction base="CrimeLineBusiness_Type">
          <xs:sequence/>
        </xs:restriction>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

redef2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="redef1.xsd">
    <xs:complexType name="CrimeLineBusiness_Type">
      <xs:complexContent>
        <xs:extension base="CrimeLineBusiness_Type">
          <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="CrimeSchedule"/>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="CrimeMoneyAndSecurities"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

一个有效的XML实例test.xml

<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="redef2.xsd">

  <CrimeSchedule></CrimeSchedule>
  <CrimeSchedule></CrimeSchedule>
  <CrimeMoneyAndSecurities></CrimeMoneyAndSecurities>
  <CrimeMoneyAndSecurities></CrimeMoneyAndSecurities>

</test>

问候,乔治

猜你在找的XML相关文章