Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating XML with XSD

I'm running into real difficulties validating XML with XSD. I should prefix all of this and state up front, I'm new to XSD and validation, so I'm not sure if it's a code issue or an XML issue. I've been to XML API hell and back with the bajillion different options and think that I've found what would be the ideal strategy for validating XML with XSD. Note, my XML and XSD are coming from a database, so I don't need to read anything from disk.

I've narrowed my problem down into a simple sample Windows Forms application. It has a textbox for XSD (txtXsd), a textbox for XML (txtXml), a textbox for the result (txtResult), and a button to start the validation (btnValidate).

I'm using a sample XSD file from Microsoft,

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema">
    <xsd:element name="title" type="xsd:string" />
    <xsd:element name="comment" type="xsd:string" />
    <xsd:element name="author" type="authorName"/>
    <xsd:complexType name="authorName">
        <xsd:sequence>
            <xsd:element name="first-name" type="xsd:string" />
            <xsd:element name="last-name" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

I'm using the following code in my application.

private void btnValidate_Click (object sender, EventArgs e)
{
    try
    {
        XmlTextReader reader = new XmlTextReader(txtXsd.Text, XmlNodeType.Document, new XmlParserContext(null, null, String.Empty, XmlSpace.None));
        XmlSchema schema = XmlSchema.Read(reader, null);
        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add(schema);

        XDocument doc = XDocument.Parse(txtXml.Text);
        doc.Validate(schemas, ValidateSchema);
    }
    catch (Exception exception)
    {
        txtResult.Text += exception.Message + Environment.NewLine;
    }
}

private void ValidateSchema (Object sender, ValidationEventArgs e)
{
    txtResult.Text += e.Message + Environment.NewLine;
}

As a test, I put in valid XML, but what I think should not conform to the XSD above.

<xml>
    <bogusNode>blah</bogusNode>
</xml>

The result is nothing, no validation errors whatsoever. How do I fix it?

like image 927
Joshua Belden Avatar asked Jun 03 '09 05:06

Joshua Belden


1 Answers

Well, for one - your XSD defines a XML namespace xmlns="urn:bookstore-schema" which is not present in your XML test file - therefore, nothing in your XML test file will be validated.

If you remove those elements form your schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="title" type="xsd:string" />

then it will properly validate your XML test file and complain about the wrong elements.

Also using an element named <xml> might not be a great idea - since the directive <?xml ......?> is a pre-defined directive and should not appear as tag name elsewhere in your document.

Marc

like image 108
marc_s Avatar answered Oct 03 '22 03:10

marc_s