gpt4 book ai didi

xml - 如何覆盖父/扩展元素内的 Xsd 元素

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

我正在我的公司创建一个新的数据交换服务。我们想要扩展在我们的 core.xsd 定义文件中定义的现有对象。这是我需要做的一个例子:

<xs:complexType name="parentType">
<xs:sequence>
<xs:element name="departmentName" type="core:DEPARTMENT_NAME"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="childType">
<xs:complexContent>
<xs:extension base="parentType">
<xs:sequence>
<xs:element name="departmentName"
type="core:DEPARTMENT_NAME"
minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

我认为这非常有道理。我想覆盖父元素并使其成为必需元素。但是,一个有效的 xml 文件应该是这样的。现在哪里多了一个部门名称!?

<childType>
<departmentName>HR</departmentName>
<departmentName>IT</departmentName>
</childType>

我怎样才能使 XML 文件变成:

<childType>
<departmentName>IT</departmentName>
</childType>

谢谢,克雷格

最佳答案

您需要使用限制而不是扩展。对于您指定的场景,这将是一个完全有效的模式(我已经大量使用 namespace 使其有效)。

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:core="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="parentType">
<xs:sequence>
<xs:element name="departmentName" type="core:DEPARTMENT_NAME" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="childType">
<xs:complexContent>
<xs:restriction base="parentType">
<xs:sequence>
<xs:element name="departmentName" type="core:DEPARTMENT_NAME"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>

<xs:simpleType name="DEPARTMENT_NAME">
<xs:restriction base="xs:string"/>
</xs:simpleType>

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

关于xml - 如何覆盖父/扩展元素内的 Xsd 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952721/

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