Okay, I have read several similar questions on stack overflow and google but none seem to be the correct answer I am looking for...
We have several different clients that provide us XML feeds. We provide every client with a document that tells them, at a minimum, they must provide us an
<id>
<name>
<updated_at>
tags in each of the child nodes in their feed. Basically, we are trying to standardize the process. Each client will usually add in a bunch of other tags that are optional. We check if those tags exist and load the data if they do, and ignore them if the tag does not exist. Also, in the client feeds, I am trying to leave it open so they can deliver their feeds with the tags in any order... but honestly, I am starting to think that the solution to my issue is that part of the definition of a "standard" feed is for them to deliver the feed in a certain order.
Anyway...
So, for example, I might have a feed that looks like this for client A
<?xml version="1.0" encoding="UTF-8"?>
<location>
<id>10794</id>
<name>Location Name 1</name>
<short_name>Short Name for Location 1</short_name>
<description>blah blah blah blah lots of test here</description>
<image>http://ift.tt/1v9ZGib;
<customattr1>30.3348048</customattr1>
<customattr2>-81.6671269</customattr2>
<updated_at>Mon, 03 Feb 2014 19:53:27 +0000</updated_at>
</location>
<location>
<id>96</id>
<name>Location 2</name>
<short_name>Location 2 Short Name</short_name>
<description>more long text for the description</description>
<image>http://ift.tt/1v9ZGyt;
<customattr1>30.3250000</customattr1>
<customattr2>-81.6624480</customattr2>
<updated_at>Mon, 03 Feb 2014 19:53:27 +0000</updated_at>
</location>
<location>
....
</location>
etc...
</locations>
And, then client B might provide us a feed like
<?xml version="1.0" encoding="UTF-8"?>
<location>
<id>10794</id>
<name>Location Name 1</name>
<short_name>Short Name for Location 1</short_name>
<description>blah blah blah blah lots of test here</description>
<updated_at>Mon, 03 Feb 2014 19:53:27 +0000</updated_at>
<image>http://ift.tt/1v9ZGib;
</location>
<location>
<id>96</id>
<name>Location 2</name>
<short_name>Location 2 Short Name</short_name>
<description>more long text for the description</description>
<updated_at>Mon, 03 Feb 2014 19:53:27 +0000</updated_at>
<image>http://ift.tt/1v9ZGib;
</location>
<location>
....
</location>
etc...
</locations>
So, I have been tasked to write an XSD to provide to our clients so they can validate their XML against our XSD before providing us the feed. Currently, we have to customize every feed for every client.
I starting learning XSD and quickly discovered that if I use
<xs:all>
I can check for the required tags
<id>
<name>
<updated_at>
in any order, but I cannot use the
<xs:any processContents="skip" />
to skip all the other optional tags. If I use
<xs:sequence>
I can use the xs:any but now my required tags have to be in a very distinct order.
When using all, the XSD validated just fine,
<xs:all>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="updated_at" type="xs:string"/>
</xs:all>
but, when I apply the XSD to the XML for Client A above, i get these errors Error - Line 8, 19: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 19; cvc-complex-type.2.4.a: Invalid content was found starting with element 'short_name'. One of '{updated_at}' is expected. for each
And when I create this XSD
<xs:all>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="updated_at" type="xs:string"/>
<xs:any processContents="skip" />
</xs:all>
The XSD does not even validate and I get this error Not valid. Error - Line 12, 39: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 39; s4s-elt-must-match.1: The content of 'all' must match (annotation?, element*). A problem was found starting at: any.
I have tried numerous variations with no luck.
Am I capable of checking for the 2 requirements listed below using XSD???
, , tags are required (once and only once, I am using the minOccurs and maxOccurs but left out above for space since my post already seems excessive for what I thought was a very simple requirement) for each and can be in any order.
All other tags are optional and can be ignored
And if so, how in the world do I write that complexType???
This seems so simple to me and yet, here I am, 1 day later totally baffled. There is definitely much more to XSD than I ever imagined.
Thanks in advance for your recommendations.
No comments:
Post a Comment