• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Need help with XSD to validate incoming XML  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Background: I am a C# developer and I am developing a Web Service that will take an XML string as input for various household Appliances and associated details about each model of appliance.
So I am learning more about XML than I ever wanted to and in that process discovered XSD and how awesome it is the validate against the incoming XML. So I am close but not prize with my XSD
and I need help with my specific situation.


So here is how the a valid inbound XML string.


Now here is my XSD coding:


So I want ApplianceModels to wrap all Models and to be able to specify an ApplianceType attribute in the ApplianceModels tag.
Within each Model tag I want to be able to have various fields (sub-tags) about each Model. There are 4 tags that are mandatory
(Action, Brand, ModelNumber, ManufacturerCode) and are the same mandatory tags for any ApplianceType (I want to be able to
use this same xsd for any of about 50 ApplianceTypes). I want to be able to have the mandatory tags be in any order (so I used xsd:all)
but there can only be 1 of each tag within a model (so I set minOccurs and MaxOccurs accordingly).
This may be the difficult part, I also want to be able to have extra tags(fields) along with the mandatory ones. These
extra tags will vary depending on the ApplianceType. I dont need checking on those really but I need to be able to have
as many as I need with no limits. There could 1 or 100 extra of optional these tags and could named just about anything. Is this even
possibly with xsd validation or am I going to have to do this in C# programmatically the hard way.

I hope I am posting this to the right place. Any help would be most appreciated. I have tried many different combinations but can't
get it to validate properly with online XML validators. Thank you in advance. This is my very first XML and XSD project so
please be patient with my 1st attempt at the XSD.





 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D Cuff wrote:I also want to be able to have extra tags(fields) along with the mandatory ones.


Are those the size, doortype, and color elements in your example?

If you don't want to make them mandatory, then you should be able to specify a minOccurs="0".  Also, I think you will need to specify maxOccurs for the Model element, otherwise it will be limited to just one.
 
Ron McLeod
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
D Cuff
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes in my example XML size, door and color are the extra elements but they are not real elements I just put them in the example as samples. The number of different optional elements could be in the thousands.
That's why I didn't want to have to put those in the XSD. I was hoping there was a way to state in XSD, to just allow any other tags in addition to the mandatory ones.
 
D Cuff
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do have to state every tag that will be under Model, then that means I will need to create a different xsd file for every possible ApplianceType and list all the Model sub tags that are specific to that ApplianceType.
Do you think that is the case Ron? or is there a way to state in addition to the stated tags, accept any other various named tags as well?
I do know all the fields for each appliance type but I was trying to avoid having to make a new file for each appliance type. In some cases a single ApplianceType has 80 to 100 fields.
 
Ron McLeod
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I work with XML, but I am not an expert, so take this suggestion with that understanding.  If this isn't exactly what you're looking for, maybe it can provides some ideas.

What about creating a child element in Model as a container for the extra information, and within that container, individual elements for each extra piece of information, with an attribute for the name of the type of information.


 
D Cuff
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just found what I think is what I need.

<xsd:any minOccurs="1" maxOccurs="unbounded" />


But I tried putting this tag after the ManufacturerCode element tag. The validator didn't like it placed there.
It said "The content of 'all' must match (annotation?,elementr*).  A problem was found stating at: any"

Ron: your idea of an Extras tag is interesting. I had not thought of that. If I cannot get the <any> tag to work
then I may consider that. Thank you for the help and the welcome as well.
 
D Cuff
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I just figured out that having an xsd:any within an xsd:all is not allowed in XSD 1.0 but will be allowed in 1.1.
So I think I will need to consider your idea of the extra sub tags to make this work. Thanks again!
 
Ron McLeod
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D Cuff wrote:So I just figured out that having an xsd:any within an xsd:all is not allowed in XSD 1.0 but will be allowed in 1.1.


You could have a sequence of xsd:any inside the extras container/wrapper.  Since you want to avoid describing the elements for the extra information in the schema, you would need to specify processContents="lax" (or processContents="skip").


 
D Cuff
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron,
I guess we came to the same conclusion once you gave me the extras idea. I had decided to go with this:

 
D Cuff
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again. You saved me a lot of time and brain cells.
 
Ron McLeod
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome.   And thanks for replying back with your solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!