Jump to content

XSD Defaults Populate XML document


sauronlord

Recommended Posts

Hi,I'm new to XML, XSD and XSLT.I was wondering if there is a way to "fill out" an XML document with its default values, as if "expanded" so that my XSLT processor could use these defaults in generating the output.I think if I had a schema-aware XSLT processor (SAXON-SA), then I could get this done easily as the transformation would be done against an XML document validated against an XSD.That is not an option for my company for various reasons.Is there a 2-stage process I can execute (in Java SAX/DOM?) that will "fill out" the required default values for elements or attributes?I see there are validator classes, but none of these allow me to "write back" the resultant XML(with defaults populated) so that I may use the "expanded" form in subsequent processing.Thanks,

Link to comment
Share on other sites

I'm not sure I follow what you mean by "default values" and "expanded".Do you want to generate a new XML that is valid according to a selected XML Schema? If so, you could use Stylus Studio. It has that ability. However, it's an IDE, not a program you could use to automate such process.But really, what's the big scenario? Why would you need to do that?

Link to comment
Share on other sites

I'm not sure I follow what you mean by "default values" and "expanded".Do you want to generate a new XML that is valid according to a selected XML Schema? If so, you could use Stylus Studio. It has that ability. However, it's an IDE, not a program you could use to automate such process.But really, what's the big scenario? Why would you need to do that?
I'm sorry for being vague.What I mean is that I have a schema that defines default values for certain attributes or elements, but the XML document that I provide omits these attributes/elements --> and I want to be able to grab the default values as if they were actually present in the XML document.For example:Say I have defined the element:<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string" required="yes" default="Canada"/> </xs:sequence> </xs:complexType></xs:element>Now Say that I have this XML snippet:...<shipto> <name>John Doe</name> <address>123 St.</address> <city>Waterloo</city></shipto>...Since element "country" is required and has a default specified, I would imagine that the "country" element is available to me (say SAX or DOM processing) as if it was actually present in the "shipto" element. I can figure out how to validate an XML document against an XSD in Java, however I do not see a way to "write back" the completed (defaults populated) document so that I can use it for something else.Is there anyway to do this short of writing a routine to walk through the XSD and populate the elements in the XML?Thanks for your help,
Link to comment
Share on other sites

First and most important, your sample Schema is not valid. It should be:

<xs:element name="shipto"><xs:complexType><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string" minOccurs="1" maxOccurs="unbounded" default="Canada"/></xs:sequence></xs:complexType></xs:element>

Do get Stylus Studio or another good Schema editor as it can validate Schema files for you and has plenty of other great stuff.I think I got your point now. You want sort of a gracefull error handling. That is, recovery of recoverable errors. Good. I'd try to do such thing in XSLT, but considering Schema's cluttered syntax, I'd say this would be a pain to implement. And it's not going to be less of a pain in any other language. What's worse is that there is no pre made package for that.SAXON-SA is not a solution for you I think. At least not exactly. Even SAXON-B would be just fine.Well, on the very least, you can always force the application that generates the XML to fill in the default when such is absent and THEN validate the XML document. How you do that very depends on how was the input generated. In XForms for example, you can define a default value in the form itself. If generated with DOM or another method... I'm afraid it will have to be hardcoded. Fetching the value is doable, but again, because of Schema's complexity, it's unreliable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...