开发者

Is it possible to use groovy.xml.MarkupBuilder with a given DTD?

The question sums it up quite well, anyway, superfluous details are never that superfluous.

I want to create Apache Forrest documents using groovy.xml.MarkupBuilder. I obviously want my 开发者_JS百科documents to be not only well-formed, but also valid according to Forrest DTD. How can I ensure that ?


Good question. AFAIK, the only way is to validate your XML against the DTD after the nodes are generated. Typically this would be by a separate process. See this article for how to do that in Java (depending on which version).

But to clarify: given that you are in full control of generating the XML, how do you want MarkupBuilder to help ensure validity ... throw an exception if you try to create a node that's not allowed at a particular point? Validity of an incomplete XML document is not a well-defined concept. What if the invalidity is that a required element is missing... when you create its parent node, its children will all be "missing", so how would MarkupBuilder know when to decide the XML is invalid? Or what if you emit an attribute of type ID or IDREF, and you can't check for uniqueness or referential integrity until all other IDs are emitted?

I can't imagine a process that would really work for guaranteeing valid XML output, that is significantly different from validating the output after it's generated.

There are definitely ways to partially validate during output, i.e. to catch some problems that would lead to invalid output.

However, Schema-aware XSLT apparently can do complete validation during generation of the XML. The article at http://www.ibm.com/developerworks/xml/library/x-schemaxslt.html#validout talks about using Schema-aware XSLT transformations to guarantee that the output is valid. I'm not sure entirely how that works. Maybe it validates what it can during generation, but for some constraints it has to wait till the output is finished before pronouncing it valid or not.

That doesn't answer your question about Groovy MarkupBuilder, but it sheds light on what is possible. AFAIK, there are no free XSLT processors that provide schema-aware processing; but Saxon-SA has a 30-day trial, and probably other commercial ones do too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜