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.
This is my XML file:
<?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:
<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>
It's perfectly valid. @Robert: It's not violating any rule, because there are no rules with that document. Therefore it can not be valid (nothing to validate against). It's just well-formed XML.
While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots.
The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.
Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With