0

I have an xml that I wanted to verify against an xsd (with multiple schemas). The xml is as follows:

    <?xml version="1.0" encoding="utf-8"?>
<Interchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.abc.com.au/eApplications/Interchange/4.2">
  <InterchangeID>19768</InterchangeID>......</Interchange>

This xml didn't pass the validation and was giving me the following error:

The 'http://www.abc.com.au/eApplications/Interchange/4.2:Interchange' element is not declared.

When I introduced namespace with xml like this:

xmlns:ns="http://www.abc.com.au/eApplications/Interchange/4.2"

The error was gone, even I didn't introduce namespace prefix like or with any of the xml tags like:

<ns:Interchange>
   <ns:InterchangeID>...

My Findings: The xml passes the validation if I either introduce :ns as a namespace or remove the line completely (after the parent Interchange tag) containing all the namespace references defined and keep it like that:

<Interchange>
  <InterchangeId>....

which is obviously not good.

Though I was able to fix this by the method explained above, I want to know the reason behind this as I couldn't find it after going through many links this, A Ten-Minute Guide to XML Namespaces and XML Namespaces Explained

Edit I'm attaching the xsd here:

enter code here
<xs:schema xmlns:eapp="http://www.abc.com.au/eApps/iChange/4.2"
     elementFormDefault="qualified"
      targetNamespace="http://www.abc.com.au/eApps/iChange/4.2"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:include schemaLocation="abc_datatypes.xsd"/>
      <xs:complexType name="abcType">
       <xs:sequence> </xs:sequence>
    </xs:complexType>
    </xs:schema>

  and the xml sample that passes validation is something like:


<soapenv:Envelope xmlns:soapenv="url"
    xmlns:orig="url/OServices" xmlns:ns="http://url/eApps/IChange/4.2">
  <soapenv:Header/>
  <soapenv:Body>
    <or:Originate>
       <!--Optional:-->
       <or:data>
       <ns:InID>2229</ns:InID>
       <ns:ReceiverID>CFS</ns:ReceiverID>
    </or:data>
   </or:Originate>
   </soapenv:Body>
 </soapenv:Envelope>
Community
  • 1
  • 1
sam
  • 391
  • 1
  • 5
  • 19
  • what is the XSD definition for the `Interchange` element? – kennyzx Apr 13 '17 at 01:48
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – kjhughes Apr 13 '17 at 02:01
  • When you remove the namespace prefix the root node simply becomes a completely unknown node -- and an unknown node is treated as a warning, not an error. See [XDocument.Validate is always successful](http://stackoverflow.com/q/17232575/3744182). In general any globally defined element can be a root element, see [XML Schema: root element](http://stackoverflow.com/q/8854144/3744182), which may be why this is a warning not an error. – dbc Apr 13 '17 at 03:38
  • It's coming as an error, not warning – sam Apr 13 '17 at 06:25

1 Answers1

0

tl;dr

My Findings: The xml passes the validation if I either introduce :ns as a namespace or remove the line completely (after the parent Interchange tag) containing all the namespace references defined and keep it like that:

<Interchange>
  <InterchangeId>....

which is obviously not good.

It passes validation because both methods remove the default namespace.

Additional explanation...

This xml didn't pass the validation and was giving me the following error:

The 'http://www.abc.com.au/eApplications/Interchange/4.2:Interchange' element is not declared.

You didn't supply the schema so we can't verify, but what this is saying is that the Interchange element in the `` namespace does not exist in the schema.

When you declare a namespace without a prefix (xmlns="some uri") that becomes the default namespace for that context.

Since you say there aren't any validation errors when you remove xmlns="http://www.abc.com.au/eApplications/Interchange/4.2" completely, this leads me to believe that Interchange is not in a namespace in the schema.

When I introduced namespace with xml like this:

xmlns:ns="http://www.abc.com.au/eApplications/Interchange/4.2"

The error was gone,...

That's because you bound the namespace uri http://www.abc.com.au/eApplications/Interchange/4.2 to the prefix ns and now there is not a default namespace.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • I'm sure I have the right xml built-up, but probably I'm confusing by passing the wrong startup tag (like 'Interchange') as this was generated automatically by using the .cs provided with xsd. – sam Apr 18 '17 at 01:53