2

I have an XML file that need to be validate with the XSD file, but when I got this error when I want to validate my XML with XSD file

Cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element 'ClientData'. One Of '{"http://www.myTest/xml/Import/user/data":ClientData}' Is Expected., Line '2', Column '27'.

This is my XML File

<?xml version="1.0" encoding="UTF-8"?>
<prefix:UteXmlComunicazione xmlns:prefix="http://www.myTest/xml/Import/user/data">
    <ClientData>
        <client>
            <pfPg>PF</pfPg>
            <Family>Alex White</Family>
            <name></name>
        </client>
        <vendor>
            <Timeperiod>
                <NumberofFactor></NumberofFactor>
                <year>2018</year>
            </Timeperiod>
            <Address>
                <Address1>
                    <top>Via</top>
                    <street>DANTE</street>
                    <number>108</number>
                    <Zipcode>20776</Zipcode>
                    <Code>033032</Code>
                </Address1>
            </Address>
        </vendor>
    </ClientData>
</prefix:UteXmlComunicazione>

and this is the XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.myTest/xml/Import/user/data" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UteXmlComunicazione">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ClientData" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="client" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="pfPg"/>
                    <xs:element type="xs:string" name="Family"/>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="vendor" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="TimePeriod" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:short" name="year"/>
                          <xs:element type="xs:byte" name="NumberofFactor"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="Address" minOccurs="0">

I have no idea what is the issue...Could you please let me know what is the problem?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
sinaei
  • 491
  • 2
  • 5
  • 19

2 Answers2

2

Your immediate error is due to ClientData not being in the target namespace of the XSD. It appears that you intended to declare a default namespace on the root element, but you only changed the namespace of the root element.

Change

<prefix:UteXmlComunicazione xmlns:prefix="http://www.myTest/xml/Import/user/data">

to

<UteXmlComunicazione xmlns="http://www.myTest/xml/Import/user/data">

to fix your immediate error.

Here is an updated copy of your XML that will validate against an updated copy of your XSD. (The Address element has been elided since you've not included its definition in your XSD, and it's not worth building out here.)

XML

<?xml version="1.0" encoding="UTF-8"?>
<UteXmlComunicazione 
    xmlns="http://www.myTest/xml/Import/user/data"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.myTest/xml/Import/user/data try.xsd"
    >
  <ClientData>
    <client>
      <pfPg>PF</pfPg>
      <Family>Alex White</Family>
      <name></name>
    </client>
    <vendor>
      <TimePeriod>
        <year>2018</year>
        <NumberofFactor>0</NumberofFactor>
      </TimePeriod>
    </vendor>
  </ClientData>
</UteXmlComunicazione>

XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
           targetNamespace="http://www.myTest/xml/Import/user/data"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UteXmlComunicazione">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ClientData" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="client" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="pfPg"/>
                    <xs:element type="xs:string" name="Family"/>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="vendor" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="TimePeriod" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:short" name="year"/>
                          <xs:element type="xs:byte" name="NumberofFactor"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Hi, Actually I added the prefix because if I dont mention the prefix in my root element, I see this error: Cvc-elt.1.a: Cannot Find The Declaration Of Element 'UteXmlComunicazione'., Line '1', Column '113'. – sinaei May 20 '20 at 12:23
  • That's a different problem. Use whatever mechanism your validating XML parser has to associate your XML doc with your XSD, or provide a hint in your XML via `schemaLocation`. See [How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?](https://stackoverflow.com/q/35411871/290085). – kjhughes May 20 '20 at 12:29
  • (Note that I've shown how to use `schemaLocation` in the sample XML of this answer, but see the link in the above comment for a detailed explanation of the mechanism.) – kjhughes May 20 '20 at 12:32
  • Thanks, I am not sure if I got your point... ```schemaLocation ``` should be the name of my XSD file? means that try.xsd should be the name of my XSD file...Since I do not have access to the validation system of our user, I validate it with online free tool. I try to set it as the name of my XSD file but I see all the error not only just for ```clientData ```, but also for the other elements.... Maybe the Online tool is not working good? – sinaei May 20 '20 at 12:46
  • 1
    No, `try.xsd` is just an example; use the name of your own XSD. If the online tool has a way to associate an XSD with an XML file, then you won't even need `schemaLocation`. – kjhughes May 20 '20 at 13:26
1

In the XSD you have elementFormDefault="qualified" this makes it so all elements needs to be qualified and not only the root element. Seeing there is no prefix on your ClientData in the XML this does not work.

So or make all elements qualified in the XML or change de XSD elementFormDefault value.

https://www.w3schools.com/xml/el_schema.asp

elementFormDefault Optional. The form for elements declared in the target namespace of this schema. The value must be "qualified" or "unqualified". Default is "unqualified". "unqualified" indicates that elements from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that elements from the target namespace must be qualified with the namespace prefix

Nick Hol
  • 302
  • 1
  • 10
  • Thanks Nick, so you mean that I need to add prefix for all my elements? – sinaei May 20 '20 at 12:11
  • This is not the actual problem. See [my answer](https://stackoverflow.com/a/61912809/290085) for details about what's really wrong. – kjhughes May 20 '20 at 12:12
  • And see [this explanation about what `elementFormDefault` means](https://stackoverflow.com/a/46758277/290085) – kjhughes May 20 '20 at 12:16
  • sinaei that is possible. But setting the default namespace as @kjhughes suggested is a cleaner solution. – Nick Hol May 20 '20 at 12:22