1

I am trying to validate the following XML using a schema.

<?xml version="1.0" encoding="ISO-8859-1"?>
<Noticias xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="UXMLS-5.xsd">
  <Noticia dia="12" mes="3" año="2004" seccion="Nacional">
    <medio tipo="radio">Onda Cero</medio>
    <periodista>Fermín Bocos</periodista>
    <titular>Atentado bestial en Madrid</titular>
    <resumen>Resumen de la noticia Atentado bestial en Madrid</resumen>
  </Noticia>
</Noticias>

and I am stuck when validating the below line from the above:

<medio tipo="radio">Onda Cero</medio>

I am capable of validating "Noticia", which has attributes, as it isn't a final element with a type associated, I can't when it is the child element with content associated to it though.

I tried many approaches, with not luck so far, also I wasn't able to find a solution online. Any ideas? Am I missing something or is it just not possible doing it?

UPDATE: This is the XSD so far, keep in mind that the "medio" bit is wrong.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Noticias">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Noticia" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="medio" type="xs:string">
                <xs:attribute name="tipo" type="xs:string">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="radio"/>
                      <xs:enumeration value="prensa"/>
                      <xs:enumeration value="television"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:element>
              <xs:element name="periodista" type="xs:string"/>
              <xs:element name="titular" type="xs:string"/>
              <xs:element name="resumen">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[a-zA-Z0-9]{60}"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="dia" type="dia"/>
            <xs:attribute name="mes" type="mes"/>
            <xs:attribute name="año" type="xs:integer"/>
            <xs:attribute name="seccion" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="dia">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="12"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="mes">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="31"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Maria
  • 13
  • 3

1 Answers1

0

There are two problems with your validation markup:

  1. You've been missing xs:complexType and xs:simpleContent elements around your xs:attribute. And an <xs:extension base="xs:string"> for specifying the value type of the content of the element like explained here at W3Schools.

So the medio code should look like this:

<xs:element name="medio">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="tipo">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="radio"/>
              <xs:enumeration value="prensa"/>
              <xs:enumeration value="television"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
  1. Another problem(not part of your question): Your xs:pattern should be from 1 to 60 chars instead of exactly 60 chars and include spaces:

    <xs:pattern value="[\sa-zA-Z0-9]{1,60}"/>
    

Now your XSD should work as exprected.

zx485
  • 28,498
  • 28
  • 50
  • 59