0

I have this XML file and I must validate it with a XSD file. I am creating the XSD file but I can't do it.

A example of my XML file

<datos>
<elemento tipoelemento="CABECERA">
    <atributo>
        <nombre>VERSION</nombre>
        <valor>1.0</valor>
    </atributo>
    <atributo>
        <nombre>BRIGADA</nombre>
        <valor>JADSJL</valor>
    </atributo>
    <atributo>
        <nombre>BUZON</nombre>
        <valor>ASDKLFJKA</valor>
    </atributo>
</elemento>
<elemento tipoelemento="INT">
    <atributo>
        <nombre>EQUNR</nombre>
        <valor>9879979797764644</valor>
    </atributo>
    <atributo>
        <nombre>ZDPYC</nombre>
        <valor>N</valor>
    </atributo>
    <atributo>
        <nombre>ZDPATORD</nombre>
        <valor />
    </atributo>
</elemento>
<elemento tipoelemento="EXT">
    <atributo>
        <nombre>zaufnr</nombre>
        <valor>54737674674</valor>
    </atributo>
    <atributo>
        <nombre>zhoras</nombre>
        <valor>6</valor>
    </atributo>
    <atributo>
        <nombre>zpuesto</nombre>
        <valor>sdgfsg</valor>
    </atributo>
</elemento>

My XSD file

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
       elementFormDefault="qualified"
       vc:minVersion="1.1">
<xs:element name="datos">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="elemento" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="elemento" minOccurs="1" maxOccurs="1" type="CABECERA" />
                        <xs:element name="elemento" minOccurs="0" maxOccurs="unbounded" type="INT" />
                        <xs:element name="elemento" minOccurs="0" maxOccurs="unbounded" type="EXT" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="CABECERA">
    <xs:sequence>
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="VERSION" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BRIGADA" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BUZON" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="VERSION">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="VERSION" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="BRIGADA">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BRIGADA" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="BUZON">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BUZON" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="INT">
    <xs:sequence>
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="EQUNR" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="ZDPYC" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="ZDPATORD" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="EQUNR">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="EQUNR" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="ZDPYC">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="ZDPYC" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="ZDPATORD">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="ZDPATORD" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="EXT">
    <xs:sequence>
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zaufnr" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zhoras" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zpuesto" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="zaufnr">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zaufnr" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="zhoras">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zhoras" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="zpuesto">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zpuesto" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
</xs:schema>

How can I validate the following points in this XSD?

  1. Always, the element with elementtype="CABECERA" have those same attribute.name and this element is required
  2. Always, the element with elementtype="INT" have those same attribute.name, but this element is optional.
  3. Always, the element with elementtype="EXT" have those same attribute.name, but this element is optional.

To create this xsd I have followed this link: click here

but this xsd throws an exception:

cos-element -istent: Error para el tipo 'CABECERA'. Aparecen en el grupo de modelos varios elementos con el nombre 'atributo' y con tipos diferentes.

I can´t abandon this design, I should validate this xml.

could somebody help me?

tripossi
  • 166
  • 10
  • The immediate validation error (*Invalid content was found starting with element 'element'. No child element is expected at this point.*) can be easily fixed by changing `xs:choice/@maxOccurs` to be `unbounded`, but you have a [more serious design problem](https://stackoverflow.com/a/62598564/290085) that should be addressed. – kjhughes Jun 26 '20 at 16:11

1 Answers1

1

First and foremost, the XML design you're trying to represent via XSD is misguided. An element named element is a serious sign of a design shortcoming (unless you're writing an XSD for XSD). You really should just abandon this design.

XSD generally expects to be able to differentiate an element's type based upon its name. Rather than

<element elementtype="HEADER">
  <attribute>
   <name>version</name>
   <value>1.0</value>
  </attribute>
  <attribute>
   <name>imei</name>
   <value>79873489274902475</value>
  </attribute>
</element>

your XML would be better designed as this:

<HEADER version="1.0" imei="79873489274902475"/>

If you insist on the current, excessively meta design, realize that you will not be able to differentiate between same-named elements according to attribute values unless you're able to use XSD 1.1, which supports Conditional Type Assignment (CTA).

Note, however, that even if you can use CTA, overdoing it as pervasively as would be required to support such an extreme meta design would still be ill-advised.

Related:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Sorry @kjhughes. I need to validate an xml with that design, When I take over the task, I will read the links in case it help me. – tripossi Jun 30 '20 at 12:03
  • If you cannot change the XML design, then you'll need more than XSD 1.0 to validate content models based on attribute values: XSD 1.1 or application code, for example. – kjhughes Jun 30 '20 at 23:18
  • In the coming days, I will take up the task and I'll analyze the xsd 1.1. Thank you for your help, Kjhughes. – tripossi Jul 01 '20 at 06:27
  • Hi, it doesn't support XSD 1.1, I receive an error about the content of the element wrapping the xs:alternative not matching the expected XSD 1.0 content model ((annotation?, (simpleType | complexType)?, (unique | key | keyref)*)) How can i fix this? – tripossi Jul 01 '20 at 12:05
  • Choose one of the following, best alternatives first: (1) Fix XML design; (2) Switch to processor that supports XSD 1.1; (3) (a) Write application-level validation code, or (b) forego checks requiring CTA. If you cannot do (1) or (2), choose between (3a) or (3b). – kjhughes Jul 01 '20 at 12:13
  • The Java code doesn't change anything; your options remain as described (1-3). Please accept and upvote this answer first as having answered usefully at the XSD level, then if you opt for 3a, post a *new question* where you include a Java application validation attempt (consider using XPath) as a [mcve] and ask how to get past a specific impasse. Thank you. – kjhughes Jul 01 '20 at 12:44
  • I have edited my question as a minimal reproducible example. Thank you @kjhughes – tripossi Jul 02 '20 at 06:53
  • Again, you've taken this line of inquiry as far as it can go given your stance on (1-3). If you wish to ask about 3a, you'll have to post a new question. I won't extend this exchange any further here. Good luck. – kjhughes Jul 02 '20 at 13:27
  • Gracias kjhughes but I do not understand what you say, sorry! – tripossi Jul 03 '20 at 06:05