How do I specify a list of required xml child elements that DONT need to be in sequence?

StackOverflow https://stackoverflow.com/questions/4304866

  •  29-09-2019
  •  | 
  •  

Pergunta

The following xml validates against the supplied xsd document. However when i start removing elements like from the xml it STILL validates!?

How do I write an xsd that forces the inclusion of elements?

<?xml version="1.0" encoding="UTF-8"?>
<Video>
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>

Then I have the xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UploadXSD"
    targetNamespace="http://tempuri.org/UploadXSD.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/UploadXSD.xsd"
    xmlns:mstns="http://tempuri.org/UploadXSD.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Video">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Title" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Description" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Contributor" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Subject" minOccurs="1" type="xs:string"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
Foi útil?

Solução

Your targetNamespace in the xsd should match the namespace of the xml you are trying to validate, i.e.

<Video xmlns="http://tempuri.org/UploadXSD.xsd">
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>

Outras dicas

If you use xs:all as opposed to xs:sequence, the elements may appear in any order. There are some additional restrictions - for example, elements can not be specified multiple times using all (I'm not sure if you intend that usage, your schema would currently allow that.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top