Xsd Verification Problem

I have the following (erroneous) Xml:

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

The id and node state are required elements. I wrote 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 is still being tested as structurally sound 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");
        });
+3
source share
3 answers

Please format your xml to make it easier to read - like this:

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

I think that you do not actually validate it - namespaces mean that this XML does not validate, even with <state>"in the second <job>". In particular, XSD has a target namespace of " http://foo.org/importvalidator.xsd", but XML does not have a namespace.

XSD XML, , , - , , .

, XSD close , - : -)


:

<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">

, :

<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: - , / SO?

+3

@13ren . , node - . . :

private static void ValidateDocument(XmlSchemaSet schemas, string uri)
{
    var settings = new XmlReaderSettings
                       {
                           Schemas = schemas,
                           ValidationFlags =
                               XmlSchemaValidationFlags.
                                   ProcessIdentityConstraints |
                               XmlSchemaValidationFlags.
                                   ReportValidationWarnings,
                           ValidationType = ValidationType.Schema
                       };
    settings.ValidationEventHandler += OnValidationEventHandler;
    using (var validatingReader = XmlReader.Create(uri, settings))
    {
        XDocument.Load(
            validatingReader,
            LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
    }
    return;
}

:

. . : "" . : . : . : "" . : .

XML :

<?xml version="1.0" encoding="utf-8" ?>
<jobs xmlns="http://foo.org/importvalidator.xsd">
  <job>
    <id>1</id>
    <state><![CDATA[IL]]></state>
  </job>
  <job>
    <id>2</id>
  </job>
</jobs>

:

: "" " http://foo.org/importvalidator.xsd . : 'state' http://foo.org/importvalidator.xsd '.

+2

/ ? , Xerces, XSD, Java. docs, , . , . .NET, , .

- CDATA ? , AFAIK. XSD, , .

- .

0

Source: https://habr.com/ru/post/1709477/


All Articles