0

I'm trying to understand how to validate an XML document with multiple namespaces in it and not getting very far. As a simplified example of what I'm doing, I have a "root" element defined in one namespace like this:

File: "root.xsd"

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="root"/>
    <xs:element name="root">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="root">
                    <xs:sequence>
                        <xs:element ref="allowedChild"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="allowedChild"/>
    <xs:element name="allowedChild" type="allowedChild"/>
</xs:schema>

Then a "child" element defined like this:

File: "child.xsd"

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:include schemaLocation="root.xsd"/>
    <xs:complexType name="child">
        <xs:complexContent>
            <xs:extension base="allowedChild"/>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="child" type="child" substitutionGroup="allowedChild"/>
</xs:schema>

And an xml that I'd like to validate that I think should look like this:

<root xmlns="root.xsd">
    <child xmlns="child.xsd"/>
</root>

If I try to validate this in XmlSpy I get "Unable to locate a reference to a supported schema type (DTD, W3C Schema) within this document instance.".

I also tried to write some C# to validate (that's my ultimate goal). My code looks like this:

static void Main(string[] args)
{
    using (var stream = new StreamReader("Example.xml"))
    {
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += MyValidationEventHandler;
        schemaSet.XmlResolver = new XmlUrlResolver();

        schemaSet.Add(null, "root.xsd");

        XmlReaderSettings settings = new XmlReaderSettings()
        {
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
            Schemas = schemaSet,
        };

        settings.ValidationEventHandler += MyValidationEventHandler;

        XmlReader reader = XmlReader.Create(stream, settings);
        while (reader.Read())
        {
        }
    }
}

static void MyValidationEventHandler(object sender, ValidationEventArgs e)
{
}

but that gives me the following validation errors:

Could not find schema information for the element 'root.xsd:root'.
Could not find schema information for the element 'child.xsd:child'.

I was hoping that the XmlUrlResolver would find that xsd files (saved in the same folder), but I guess its not doing that???

My goal is to have multiple child.xsd type files, that are produced after compile time and resolved only at run time. Is this possible?

4

1 に答える 1

1

次のようなものが必要です。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://root.schemas.mycompany.com">

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    targetNamespace="http://child.schemas.mycompany.com">

<root xmlns="http://root.schemas.mycompany.com">
    <child xmlns="http://child.schemas.mycompany.com"/>
</root>

XSD ファイル名をネームスペース名として扱っていますが、これは正しくありません。さらに、 を使用していないためtargetNamespace、XML の型と要素は名前空間の一部ではありません。

于 2013-09-25T15:36:58.637 に答える