Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validation design question

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating an XML-based interface where clients post different XML messages to a front-controller servlet. All XML messages will have the following "base" structure:

The servlet will determine the appropriate request handler depending on the Code element's value.
Rather than trying to validate the XML myself, I figured I would use xml schema. My Java classes will use JDOM. I know how to specify, at runtime, the xsd location for the XML document through JDOM. But my problem is: How can I instruct the parser which xsd file to use if I don't know the MessageType until I've parsed the XML?
I could, I suppose, do this:
1. Parse the XML first without schema validation (or perhaps only validating the "base" structure using a "base" xsd)
2. Get the MessageType
3. Use the MessageType value to lookup the location of the xsd (e.g. MessageTypes and their associated xsd locations stored in a Map)
4. Parse the XML again, this time using the message-specific xsd.
But that means I have to parse the document twice.
Any other suggestions?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Specify all message types in the same schema document (which can of course import other schemas if it gets too big otherwise).
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I figured there must be a cleaner solution.
But how do you write your schema so that it will validate a portion (e.g. the message-specific section) based on the value of another element (e.g. TranCode in my above example)?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Blake Minghelli:
Thanks, I figured there must be a cleaner solution.
But how do you write your schema so that it will validate a portion (e.g. the message-specific section) based on the value of another element (e.g. TranCode in my above example)?

You don't. You need to specify alternative contents for your request element so that all your different messages are valid according to the schema.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks for the help
 
Those are the largest trousers in the world! Especially when next to this ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic