1

Here my XSD:

<xs:schema xmlns:tns="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3.org/1999/xhtml" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:complexType name="Statistics">
    <xs:sequence>
        <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="id" type="xs:string"/>
                    <xs:element name="date" type="xs:string"/>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="email" type="xs:string" nillable="true"/>
                    <xs:element name="Advertisers">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="id" type="xs:string"/>
                                            <xs:element name="ls_revenue_usd_equivalence" type="xs:string"/>
                                            <xs:element name="revenue_eur" type="xs:string"/>
                                            <xs:element name="revenue_usd" type="xs:string"/>
                                            <xs:element name="revenue_rub" type="xs:string"/>
                                            <xs:element name="ls_leads" type="xs:string"/>
                                            <xs:element name="revenue_usd_equivalence" type="xs:string"/>
                                            <xs:element name="leads" type="xs:string"/>
                                            <xs:element name="payout_usd" type="xs:string"/>
                                            <xs:element name="payout_rub" type="xs:string"/>
                                            <xs:element name="payout_eur" type="xs:string"/>
                                            <xs:element name="payout_usd_equivalence" type="xs:string"/>
                                            <xs:element name="profit" type="xs:string"/>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="total">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="leads" type="xs:string"/>
                                <xs:element name="payout_usd" type="xs:string"/>
                                <xs:element name="payout_rub" type="xs:string"/>
                                <xs:element name="payout_eur" type="xs:string"/>
                                <xs:element name="payout_usd_equivalence" type="xs:string"/>
                                <xs:element name="profit" type="xs:string"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

Here is my XML:

 <?xml version="1.0" encoding="UTF-8"?>
 <Statistics xmlns="http://www.w3.org/1999/xhtml" attributeFormDefault="unqualified" elementFormDefault="qualified">
        <item>
            <id>1</id>
            <date>16.12.2015</date>
            <name>test</name>
            <email>test</email>
            <Advertisers>
                <item>
                    <id>2</id>
                    <ls_revenue_usd_equivalence>0</ls_revenue_usd_equivalence>
                    <revenue_eur>0</revenue_eur>
                    <revenue_usd>0</revenue_usd>
                    <revenue_rub>0</revenue_rub>
                    <ls_leads>0</ls_leads>
                    <revenue_usd_equivalence>0</revenue_usd_equivalence>
                    <leads>0</leads>
                    <payout_usd>0</payout_usd>
                    <payout_rub>0</payout_rub>
                    <payout_eur>0</payout_eur>
                    <payout_usd_equivalence>0</payout_usd_equivalence>
                    <profit>0</profit>
                </item>
            </Advertisers>
            <total>
                <leads>0</leads>
                <payout_usd>0</payout_usd>
                <payout_rub>0</payout_rub>
                <payout_eur>0</payout_eur>
                <payout_usd_equivalence>0</payout_usd_equivalence>
                <profit>0</profit>
            </total>
        </item>
 </Statistics>

When I trying validate this XML, I get this error:

cvc-elt.1.a: Cannot find the declaration of element 'Statistics'.

At first glance, all right, and namespaces matches. I do not understand what could still be a mistake. What am I doing wrong?

kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

1

You have defined a complex type with xs:complexType name="Statistics", but not an element of that name. You would need to define <xs:element name="Statistics"><xs:complexType>...</xs:complexType></xs:element>.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • if I can not change xsd file - then I need to remove the element from xml? – Yevhenii Shevchuk Dec 21 '15 at 19:09
  • If the schema defines no global elements then I don't see how you could write an instance document to be valid against the schema, other then maybe doing `...`. But that it is very unusual that a schema does not define any global elements. On the other hand, your target namespace is the XHTML namespace of the W3C, so I wonder anyway what the schema is trying to do with that namespace. – Martin Honnen Dec 21 '15 at 19:16
  • @MartinHonnen, IIRC FpML works a bit like this: it's normal practice to validate against a type defined in the instance document using xsi:type, rather than against a global element declaration. – Michael Kay Dec 22 '15 at 00:04
1

First of all, don't re-use a well-known namespace, http://www.w3.org/1999/xhtml, for something other than its intended purpose. You're just asking for confusion, conflicts with existing XML catalogs, etc.

Second, attributeFormDefault and elementFormDefault do not belong in your XML but in your XSD.

Third, as Martin Honnen mentions, your XSD lacks an element declaration for Statistics.

Fourth, if you really can't change that XSD but want to use it, you could do one of the following:

Here's how to use xsi:type in your case (without recommended namespace fix applied):

<?xml version="1.0" encoding="UTF-8"?>
<Statistics xmlns="http://www.w3.org/1999/xhtml"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.w3.org/1999/xhtml try.xsd"
            xmlns:tns="http://www.w3.org/1999/xhtml"
            xsi:type="tns:Statistics">
  <item>
    <id>1</id>
    <date>16.12.2015</date>
    <name>test</name>
    <email>test</email>
    <Advertisers>
      <item>
        <id>2</id>
        <ls_revenue_usd_equivalence>0</ls_revenue_usd_equivalence>
        <revenue_eur>0</revenue_eur>
        <revenue_usd>0</revenue_usd>
        <revenue_rub>0</revenue_rub>
        <ls_leads>0</ls_leads>
        <revenue_usd_equivalence>0</revenue_usd_equivalence>
        <leads>0</leads>
        <payout_usd>0</payout_usd>
        <payout_rub>0</payout_rub>
        <payout_eur>0</payout_eur>
        <payout_usd_equivalence>0</payout_usd_equivalence>
        <profit>0</profit>
      </item>
    </Advertisers>
    <total>
      <leads>0</leads>
      <payout_usd>0</payout_usd>
      <payout_rub>0</payout_rub>
      <payout_eur>0</payout_eur>
      <payout_usd_equivalence>0</payout_usd_equivalence>
      <profit>0</profit>
    </total>
  </item>
</Statistics>
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    I would underline the point about not abusing a namespace that you don't own. A schema validator is perfectly entitled to say "Ah, the XHTML namespace; I know all about that already, I don't need to bother reading this schema". – Michael Kay Dec 21 '15 at 22:50