Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a data gathering and reporting application that takes XML files as input, which will then be read, processed, and stored in a strongly-typed database. For example, an XML file for a "Job" might look like this:

<Data type="Job">
    <ID>12345</ID>
    <JobName>MyJob</JobName>
    <StartDate>04/07/2012 10:45:00 AM</StartDate>
    <Files>
        <File name="a.jpeg" path="images\" />
        <File name="b.mp3" path="music\mp3\" />
    </Files>
</Data>

I'd like to use a schema to have a standard format for these input files (depending on what type of data is being used, for example "Job", "User", "View"), but I'd also like to not fail validation if there is extra data provided.

For example, perhaps a Job has additional properties such as "IsAutomated", "Requester", "EndDate", and so on. I don't particularly care about these extra properties. If they are included in the XML, I'll simply ignore them when I'm processing the XML file, and I'd like validation to do the same, without having to include in the schema every single possible property that a customer might provide me with.

Is there a standard way of providing such a schema, or of allowing such a general XML file that can still be validated without resorting to something as naïve (and potentially difficult to deal with) as the below?

<Data type="Job">
    <Data name="ID">12345</Data>
    . . .
    <Data name="Files">
        <Data name="File">
            <Data name="Filename">a.jpeg</Data>
            <Data name="path">images</Data>
        . . .
    </Data>
</Data>
share|improve this question
    
Have you looked at XSD? –  user2567 Apr 7 '12 at 19:27
add comment

2 Answers

XSD is the way to go if your file format is XML -- this is exactly the sort of task XSD is meant for and it really does shine in this space.

To handle your optional elements you can do two things. The first is to define them as optional attributes using minOccours="0". If you've got something more complex going on you could also use the processContents="lax" directive.

share|improve this answer
add comment

I'm actually working on something similar, and I've settled with using a JSON file on the server that describes what type of elements/attributes are allowed.

When a user uploads a XML file, it can be compared to the data file on the server through a script, and elements or attributes with improper syntax can be rejected.

As far as a standard way of doing this, I'm not aware of any, though this method has been working quite well for me so far.

Here's what my test JSON object looks like:

EDIT: It's actually an object literal, but whatever ;)

XMLTemplate = {
    xml         : '<?xml version="1.0" encoding="UTF-8" ?>',
    doctype     : '<!DOCTYPE xml PUBLIC>',
    children    : [{

                   name     : "ROOT",
                   description  : "All xml documents have a root tag",


                   children : [{
                               name         : "GROUP",
                               description  : "Groups contain products, as well as some fields",
                               attributes   : [{
                                               name         : "TYPE",
                                               description  : "What type of product"
                                               },{
                                               name         : "DESCRIPTION",
                                               description  : "Group Description"
                                               }],

                               children : [{

                                           name         : "TITLE",
                                           description  : "Title of the group"

                                           },{

                                           name         : "DESCRIPTION",
                                           description  : "Group description",


                                           attributes   : [{
                                                           name         : "SOMETHINGELSE",
                                                           description  : "some other attribute I don't know"
                                                           },{
                                                           name         : "WIDTH",
                                                           description  : ""
                                                           }]

                                           },{

                                           name         : "PRODUCT",
                                           description  : "Products contain some stuffs",
                                           attributes   : [{
                                                           name : "TYPE",
                                                           description  : "type of product"
                                                           },{
                                                           name         : "SOMETHINGELSE",
                                                           description  : "some other attribute I don't know"
                                                           }],


                                           }]


                               }]
                   }]
}
share|improve this answer
    
This is interesting, but I'd like to avoid having to write custom validation for this scale of a project. –  Greg Jackson Apr 7 '12 at 18:22
    
@GregJackson Fair enough. This scheme was more for designing variable xml formats. –  Jeffrey Sweeney Apr 7 '12 at 18:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.