XML Schema - 字符串列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12493990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Schema - a list of string
提问by Jane Doe
The element had to have a type of 'a list of strings' and there can be 0or more occurrence of that type of information. So what I did was:
该元素必须具有“字符串列表”的类型,并且可以出现0或多次出现该类型的信息。所以我所做的是:
<xs:element name="xxx" type="ooo" />
<xs:simpleType name="ooo">
<xs:list itemType="xs:string" />
</xs:simpleType>
I believe this is correct, but
我相信这是正确的,但是
where do I put minOccurand maxOccurhere?
我在哪里放minOccur和maxOccur在这里?
回答by psubsee2003
Your question is unfortunately unclear because it could mean multiple things.
不幸的是,您的问题不清楚,因为它可能意味着多种含义。
One possible interpretation is that you want an element "xxx" to occur between 0 and x times. That is done by defining a sequence inside of a root element.
一种可能的解释是您希望元素“xxx”出现在 0 到 x 次之间。这是通过在根元素内定义序列来完成的。
<xs:simpleType name="ooo">
<xs:restriction base="xs:string" />
</xs:simpleType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="xxx" type="ooo" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
You cannot specify minOccurs and maxOccurs on the root element though as there can be only 1 root element in an XML. But you can define a sequence as a child to the root element, that this what is being done in the above example.
您不能在根元素上指定 minOccurs 和 maxOccurs,因为 XML 中只能有 1 个根元素。但是你可以定义一个序列作为根元素的子元素,这就是上面例子中所做的。
Now if you wanted "xxx" to be your root element, you can effectively do the same exact thing. this is exactly the same except instead of an element called "root" you now have an element called "xxx" and the child elements are called something else with a type of "ooo"
现在,如果您希望“xxx”成为您的根元素,您可以有效地做同样的事情。这完全相同,除了您现在有一个名为“xxx”的元素而不是名为“root”的元素,并且子元素被称为具有“ooo”类型的其他元素
<xs:simpleType name="ooo">
<xs:restriction base="xs:string" />
</xs:simpleType>
<xs:element name="xxx">
<xs:complexType>
<xs:sequence>
<xs:element name="xxx-child" type="ooo" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
In your example, the usage of the "list" type does mean what I think you think it means. A list type in an XSD doesn't define a sequence of elements, but a single element that can have a list of values delimited by a space.
在您的示例中,“列表”类型的使用确实意味着我认为您认为它的含义。XSD 中的列表类型不定义元素序列,而是定义可以具有由空格分隔的值列表的单个元素。
<xs:simpleType name="ooo">
<xs:list itemType="xs:string" />
</xs:simpleType>
<xs:element name="xxx" type="ooo" />
The resulting XML from this schema defination would look like this:
从此架构定义生成的 XML 将如下所示:
<xxx>item1 item2 item3 item4 item5</xxx>
The XML is effectively an unbounded list of strings of any length separated by a space. You could define a type for the list that inherits from xs:stringto restrict the types of values, but I am not aware of a method to restrict the length.
XML 实际上是一个由空格分隔的任意长度的无界字符串列表。您可以为继承自的列表定义一个类型xs:string以限制值的类型,但我不知道限制长度的方法。
And lastly, what I think you are trying to accomplish is close to one of my suggestions above with "xxx" and "xxx-child", but just reformatting where the sequence is defined.
最后,我认为您要完成的工作接近于我上面的“xxx”和“xxx-child”建议之一,但只是重新格式化定义序列的位置。
<xs:complexType name="ooo">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="child" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="xxx" type="ooo" />
And the resulting XML would like this:
生成的 XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xxx>
<child></child>
<child></child>
<child></child>
</xxx>
And there are other variations on the last option, such as moving the minOccurs="0" maxOccurs="unbounded"from the <xs:sequence>to the "child" element. In your example it won't matter at all because both definitions would result in the same exact XML. But if you had 2 child elements, it would mean different things:
而且还有最后一个选项其他变化,如移动minOccurs="0" maxOccurs="unbounded"从<xs:sequence>以“子”元素。在您的示例中,这根本无关紧要,因为两个定义都会产生完全相同的 XML。但是如果你有 2 个子元素,那就意味着不同的东西:
<xs:complexType name="ooo">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="child1" type="xs:string" />
<xs:element name="child2" type="xs:string" />
</xs:sequence>
</xs:complexType>
Would result in an XML like this (the sequence of child1 follow by child2 would be repeated x times):
会产生这样的 XML(child1 后跟 child2 的序列将重复 x 次):
<?xml version="1.0" encoding="utf-8"?>
<xxx>
<child1></child1>
<child2></child2>
<child1></child1>
<child2></child2>
<child1></child1>
<child2></child2>
</xxx>
where as
然而
<xs:complexType name="ooo">
<xs:sequence>
<xs:element name="child1" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="child2" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
would result in an XML like this (child1 would repeat x times, followed by child2 repeating y time):
将产生这样的 XML(child1 将重复 x 次,然后 child2 重复 y 次):
<?xml version="1.0" encoding="utf-8"?>
<xxx>
<child1></child1>
<child1></child1>
<child2></child2>
<child2></child2>
<child2></child2>
<child2></child2>
<child2></child2>
</xxx>
回答by Drakonoved
To avoid of getting an object, which has a field, which has a list of strings, you may want to use simpler way:
为避免获取具有字段的对象,该对象具有字符串列表,您可能需要使用更简单的方法:
<xs:complexType>
<xs:sequence>
<xs:element name="stringList" block="extension"
minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
</xs:sequence>
</xs:complexType>
then in generated class you'll get a field, which directly contains list (not an object)
然后在生成的类中你会得到一个字段,它直接包含列表(不是对象)
List<String> stringList;
回答by Firre
Here is a code sample I found that may help you:
这是我发现的一个代码示例,可以帮助您:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

