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:
在
<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
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.
如果您想指定序列中的所有元素可以出现不止一次,那么您可以在
#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:
在
<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
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.
如果您想指定序列中的所有元素可以出现不止一次,那么您可以在