user1917435
user1917435

Reputation: 23

Not able to parse XML document

I am not able to parse this xml. can anyone please help me what is wrong in this

Here is my XML file

<?xml version="1.0" encoding="UTF-8"?>
<gea-sr:GETOFFERSResponse 
  xmlns:gea-sr="http://schemas.com/soap/requests" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://schemas.com/soap/requests 
                      file:///C:/Users/Desktop/SMCWebResponse.xsd">
    <requestdate dateDescription="Wed" requestdate="2012-12-19"/>
    <requestdate dateDescription="Mon" requestdate="2012-12-24">
        <offer timeperiod="0800-1200" timeperioddesc="AM 8-12">
            <offertoken/>
            <offertext>12657131N3AM 0004 T000000000N</offertext>
            <offerremarks/>
            <offerflag>F</offerflag>
        </offer>
        <offer timeperiod="1300-1700" timeperioddesc="PM 1-5">
            <offertoken/>
            <offertext>12657131N3AA 0006 T000000000N</offertext>
            <offerremarks/>
            <offerflag>F</offerflag>
        </offer>
    </requestdate>
</gea-sr:GETOFFERSResponse>"

Here is my XSD snapshot that I am trying to use for parsing

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gea-sr="http://schemas.com/soap/requests" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:complexType name="requestdateType">
        <xsd:sequence>
            <xsd:element name="offer" type="offerType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
        <xsd:attribute name="requestdate" type="xsd:date"/>
        <xsd:attribute name="datedescription">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:maxLength value="10"/>
                    <xsd:whiteSpace value="collapse"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
    </xsd:complexType>

<xsd:complexType name="offerType">
        <xsd:all>
            <xsd:element name="offertoken" type="offertokenType"/>
            <xsd:element name="offertext" type="offertextType"/>
            <xsd:element name="offerremarks" type="offerremarksType"/>
            <xsd:element name="offerflag" type="offerflagType"/>
        </xsd:all>
        <xsd:attribute name="timeperiod" type="timespanType"/>
        <xsd:attribute name="timeperioddescription">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:maxLength value="25"/>
                    <xsd:whiteSpace value="collapse"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
    </xsd:complexType>

<xsd:simpleType name="timespanType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="0800-1200"/>
            <xsd:enumeration value="1300-1700"/>
            <xsd:enumeration value="0800-1700"/>
            <xsd:enumeration value="1000-1700"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="offertokenType">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="50"/>
            <xsd:whiteSpace value="collapse"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="offertextType">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="50"/>
            <xsd:minLength value="19"/>
            <xsd:whiteSpace value="collapse"/>
            <xsd:pattern value="[^\s].*"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="offerremarksType">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="50"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="offerflagType">
        <xsd:restriction base="xsd:string">
            <xsd:whiteSpace value="collapse"/>
            <xsd:enumeration value="A"/>
            <xsd:enumeration value="F"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:element name="GETOFFERSResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="requestdate" type="gea-sr:requestdateType" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

I get the error cvc-elt.5.2.1: The element is not valid with respect to the actual type definition '{anonymous}'. cvc-model-group: Element unexpected by type '{anonymous}' of element .

Upvotes: 1

Views: 2007

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

[First, a terminological issue: you do not have a problem parsing the XML document here; your XML is perfectly well formed and no XML parser will have any trouble parsing it. You are having trouble validating your XML document. The difference strikes some users as pedantic, but it matters to the kind of people who are likely to be able to answer questions like this, so it's to your interest to understand the distinction.]

You have several problems here.

  • In your XML instance, the element gea-sr:GETOFFERSResponse is in namespace http://schemas.com/soap/requests. But the XSD schema document you show declares an element named GETOFFERSResponse that is not assigned to any namespace at all (the schema document has no target namespace).

  • If you add targetNamespace="http://schemas.com/soap/requests" to your schema document, you will then have trouble because the declarations in the schema document refer to a number of types using unprefixed names (so the references are to types without a namespace name).

  • Adding the prefix gea-sr to the appropriate references eliminates those errors and leads to the third source of problem here: your schema says that local elements are to be in the target namespace, but in your document instance the children of GETOFFERSResponse are not namespace qualified. You can either put the children of GETOFFERSResponse into the gea-sr namespace, or you can the schema to say elementFormDefault = "unqualified".

  • Now we find that the type definition declares an attribute named datedescription for the element requestdate, but the instance uses an attribute named dateDescription. The offer element has an attribute named timeperioddescription in the schema but an attribute named timeperioddesc in the XML instance.

    Those problems can be fixed by making the schema and the instance agree.

When these problems are resolved, your XML document instance is valid.

Upvotes: 1

Related Questions