gpt4 book ai didi

xml - 什么xsd会让一个元素无限拥有自己作为子元素?

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

我怎样才能创建一个 xsd 来给我这种可以无限继续下去的 xml 结构?

<?xml version="1.0" encoding="utf-8" ?>
<SampleXml>
<Items>
<Item name="SomeName" type="string">
This would be the value
</Item>
<Item name="SecondName" type="string">
This is the next string
</Item>
<Item name="AnotherName" type="list">
<Items>
<Item name="SubName" type="string">
A string in a sub list
</Item>
<Item name="SubSubName" type="list">
<Items>
<Item name="HowDoI" type="string">
How do I keep this going infinately?
</Item>
</Items>
</Item>
</Items>
</Item>
</Items>
</SampleXml>

我找到的唯一解决方案是在 xsd 中重复我愿意复制的次数。如下所示。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SampleXml">
<xs:complexType>
<xs:sequence>
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Item">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Item">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

最佳答案

使用 <xs:element ref="bla" />递归引用元素。一个简单的例子:

<xs:element name="recursive">
<xs:complexType>
<xs:sequence>
<xs:element ref="recursive" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

允许你这样写:

<recursive>
<recursive>
<recursive />
</recursive>
<recursive />
<recursive>
<recursive>
<recursive />
</recursive>
</recursive>
</recursive>

“ref”属性的使用也大大有助于提高 XSD 的可读性。我会这样写你的:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="SampleXml">
<xs:complexType>
<xs:sequence>
<xs:element ref="Items" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="Item">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="Items" minOccurs="0" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

</xs:schema>

请注意,即使并非绝对必要(例如当 SampleXml 引用 Items 时),使用“ref”也会使 XSD 减少嵌套困惑并提高可读性。

关于xml - 什么xsd会让一个元素无限拥有自己作为子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2885514/

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