Error in XSD validation

Asked

Viewed 1,184 times

1

My XSD after some modifications by another team started giving error:

Parse Error at line 33 column 14: s4s-elt-invalid-content.1: The content of '#AnonType_TxnHdrTransaction' is invalid.  Element 'any' is invalid, misplaced, or occurs too often.

My XSD code is as follows::

<xs:element name="TxnHdr">
            <xs:complexType>
                <xs:any>
                    <xs:element name="TxnID" type="x:NumericVar15" />
                    <xs:element name="LogTxnID" type="x:NumericVar15" />
                    <xs:element name="MediaBalance" type="x:NumericVar8" />
                    <xs:element name="TxnDate" type="x:NumericFixed14" />
                    <xs:element name="TxnExtRef" type="x:AlphaNumericVar50" minOccurs="0" />
                    <xs:element name="TxnTypeX" type="x:NumericVar2" />
                    <xs:element name="TxnMode" type="x:NumericFixed1" />
                    <xs:element name="VoidStatus" type="x:NumericFixed1" />
                    <xs:element name="FailedStatus" type="x:NumericFixed1" />
                    <xs:element name="TxnSourceX" type="x:NumericVar2" />
                    <xs:element name="PurchAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="DiscAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="RdmAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="AdjAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="MediaTxnSeq" type="x:NumericVar3" minOccurs="0" />
                </xs:any>
            </xs:complexType>
        </xs:element>

1 answer

1


How does the <any>

The problem in this case is that the tag <any> is being used incorrectly. In XSD it is clear that the tag could be anything, as long as it maintains the structure of the children, but that’s not how it works.

The idea of <any> is to say that at a certain point the XML can be extended, but it is not defined there which structure.

Example

In the example then the tag is defined pessoa, including primeiroNome, ultimoNome and then the tag any allows adding any additional element:

<xs:element name="pessoa">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="primeiroNome" type="xs:string"/>
      <xs:element name="ultimoNome" type="xs:string"/>
      <xs:any minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Continuing the example, one can then define an XSD of an element filhos

<xs:element name="filhos">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="nome" type="xs:string"
      maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

So, a valid XLS for this XSD could contain the following structure:

<pessoa>
  <primeiroNome>João</primeiroNome>
  <ultimoNome>Ferreira</ultimoNome>
  <filhos>
    <nome>Gabriel</nome>
  </filhos>
</pessoa>

Note that instead of filhos, we could have any other tag defined in this or another XSD.

Or just use <xs:sequence>

If the idea is not to have a generic element as described above, just change the tag <xs:any> for <xs:sequence>.

I did some tests using a online validator and improvised some missing information in the question’s XSD. I arrived at the following valid XSD:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" xmlns:tns="http://www.w3schools.com" elementFormDefault="qualified">
   <xs:element name="TxnHdr">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="TxnID" type="x:NumericVar15" />
                    <xs:element name="LogTxnID" type="x:NumericVar15" />
                    <xs:element name="MediaBalance" type="x:NumericVar8" />
                    <xs:element name="TxnDate" type="x:NumericFixed14" />
                    <xs:element name="TxnExtRef" type="x:AlphaNumericVar50" minOccurs="0" />
                    <xs:element name="TxnTypeX" type="x:NumericVar2" />
                    <xs:element name="TxnMode" type="x:NumericFixed1" />
                    <xs:element name="VoidStatus" type="x:NumericFixed1" />
                    <xs:element name="FailedStatus" type="x:NumericFixed1" />
                    <xs:element name="TxnSourceX" type="x:NumericVar2" />
                    <xs:element name="PurchAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="DiscAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="RdmAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="AdjAmt" type="x:NegPositiveDecimalVar21_4" />
                    <xs:element name="MediaTxnSeq" type="x:NumericVar3" minOccurs="0" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
</xs:schema>
  • Got it, Luiz! I had already made the changes and fixed here, but your explanation helped me clarify things a little! XSD is a bit confusing rs! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.