gpt4 book ai didi

xml - 我可以重新定义 XSD 架构以更改某个类型的子项的 maxOccurs 属性吗?

转载 作者:数据小太阳 更新时间:2023-10-29 01:46:59 25 4
gpt4 key购买 nike

我正在尝试扩展 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>

我想让 CrimeScheduleCrimeMoneyAndSecurities 都是重复元素。我认为这可能有效(在 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.

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

谢谢!

最佳答案

当您进行扩展时,新类型的内容模型是基础内容模型的序列以及您在新类型中添加的任何内容。例如,在您的情况下,重新定义的 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 元素,然后通过扩展再次添加这些元素,并添加所需的出现次数。请参阅下面的完整工作示例:

文本.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 - 我可以重新定义 XSD 架构以更改某个类型的子项的 maxOccurs 属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/967320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com