Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xsd validation problem

Tags:

c#

xsd

I have the following (errorous) Xml:

<jobs>
    <job>
        <id>1</id>
        <state><![CDATA[IL]]></state>
    </job>
    <job>
        <id>2</id>
    </job>
</jobs>

both the id and the state node are reqired items. I wrote an Xsd for it:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="importvalidator"
    elementFormDefault="qualified"
    targetNamespace="http://foo.org/importvalidator.xsd"
    xmlns="http://foo.org/importvalidator.xsd"
    xmlns:mstns="http://foo.org/importvalidator.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="jobs">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="job" minOccurs="1" maxOccurs="unbounded">
              <xs:complexType>
                <xs:all>
                  <xs:element name="id" type="xs:string" minOccurs="1"/>
                  <xs:element name="state" type="xs:string" minOccurs="1"/>
                </xs:all>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

And it still validates as a structurally valid Xml. What am I missing here?

Update1: the code I'm using is in C#:

        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add("http://foo.org/importvalidator.xsd", "validator.xsd");

        XDocument doc = XDocument.Load(fileName);
        if (doc == null | doc.Root == null)
        {
            throw new ApplicationException("xml error: the referenced stream is not xml.");
        }

        doc.Validate(schemas, (o, e) =>
        {
            throw new ApplicationException("xsd validation error: xml file has structural problems");
        });
like image 955
balint Avatar asked Sep 22 '26 14:09

balint


1 Answers

Please format your xml so it's easier to read - like this:

<jobs>
  <job>
    <id>1</id>
    <state><![CDATA[IL]]></state>
  </job>
  <job>
    <id>2</id>
  </job>
</jobs>

I think you're not actually validating it - the namespaces mean that that XML does not validate, even with a "<state>" in the second "<job>". Specifically, the XSD has a target namespace of "http://foo.org/importvalidator.xsd", but the XML has no namespace given.

Set up a trivial test case of XSD and XML, that you definitely know will fail - use that to track down why you aren't validating.

Also, your XSD is missing the close tags for element and schema, so it should give an error - or it's just a mis-paste :-)


You can remove the targetNamespace from the schema:

<xs:schema id="importvalidator"
    elementFormDefault="qualified"
    targetNamespace="http://foo.org/importvalidator.xsd    ← DELETE THIS"
    xmlns="http://foo.org/importvalidator.xsd"
    xmlns:mstns="http://foo.org/importvalidator.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

So it looks like this:

<xs:schema id="importvalidator"
    elementFormDefault="qualified"
    xmlns="http://foo.org/importvalidator.xsd"
    xmlns:mstns="http://foo.org/importvalidator.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

PS: anyone know if/how you can highlight parts of source code with SO's markdown?

like image 74
13ren Avatar answered Sep 24 '26 02:09

13ren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!