如何在XSD中指定重复的元素

时间:2022-12-28 17:16:26

I have a an XSD which validates my XML file. The problem is that it works for one element, but when I have more than one it doesn't work and I can't find the problem.

我有一个XSD来验证我的XML文件。问题是它只适用于一个元素,但当我有多个元素时,它就不能工作,我也找不到问题。

This is my XML file:

这是我的XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <UrlList>
    <url>
        <url>https://www.youtube.com/watch?v=ke92CDVQsb8</url>
        <idUrl>72</idUrl>
        <urlShort>http://short.ly:8080/SOB/url/832261</urlShort>
        <numVisits>2</numVisits>
    </url>
    <url>
        <url>http://moodle.urv.cat/moodle/pluginfil</url>
        <idUrl>73</idUrl>
        <urlShort>http://short.ly:8080/SOB/url/45ea9b</urlShort>
        <numVisits>1</numVisits>
    </url>
</UrlList>

And this is my XSD file:

这是我的XSD文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="UrlList">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="url"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="url">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="url" type="xs:string"/>
                <xs:element name="idUrl" type="xs:integer"/>
                <xs:element name="urlShort" type="xs:string"/>
                <xs:element name="numVisits" type="xs:integer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

1 个解决方案

#1


4  

In an <xs:element> inside an <xs:sequence>, if you don't explicitly specify the maxOccurs attribute, it defaults to 1. If you want to be able to have any number of <url> elements inside your <UrlList>, you should change that part of the schema to read as follows:

中的 中,如果不显式地指定maxOccurs属性,它默认为1。如果您想要在 中包含任何数量的 元素,您应该将模式的这一部分更改为如下所示:

<xs:element name="UrlList">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="url"/>
            <!--        ^^^^^^^^^^^^^^^^^^^^^         -->
        </xs:sequence>
    </xs:complexType>
</xs:element>

If you want to only allow, say, 3 <url> elements, use maxOccurs="3" instead, and so forth.

如果您想只允许,比如,3 元素,那么使用maxOccurs="3",以此类推。

If you instead want to specify that all the elements in the sequence can appear more than once, you can set the maxOccurs attribute on the <xs:sequence> tag instead. It doesn't make a difference in this case, though, because there's only one element in the sequence.

如果您想指定序列中的所有元素可以出现不止一次,那么您可以在 标记上设置maxOccurs属性。在这种情况下,它没有什么区别,因为序列中只有一个元素。

#1


4  

In an <xs:element> inside an <xs:sequence>, if you don't explicitly specify the maxOccurs attribute, it defaults to 1. If you want to be able to have any number of <url> elements inside your <UrlList>, you should change that part of the schema to read as follows:

中的 中,如果不显式地指定maxOccurs属性,它默认为1。如果您想要在 中包含任何数量的 元素,您应该将模式的这一部分更改为如下所示:

<xs:element name="UrlList">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="url"/>
            <!--        ^^^^^^^^^^^^^^^^^^^^^         -->
        </xs:sequence>
    </xs:complexType>
</xs:element>

If you want to only allow, say, 3 <url> elements, use maxOccurs="3" instead, and so forth.

如果您想只允许,比如,3 元素,那么使用maxOccurs="3",以此类推。

If you instead want to specify that all the elements in the sequence can appear more than once, you can set the maxOccurs attribute on the <xs:sequence> tag instead. It doesn't make a difference in this case, though, because there's only one element in the sequence.

如果您想指定序列中的所有元素可以出现不止一次,那么您可以在 标记上设置maxOccurs属性。在这种情况下,它没有什么区别,因为序列中只有一个元素。