Schema API for Java?

Questions about XML that are not covered by the other forums should go here.
tobias.schoessler
Posts: 4
Joined: Wed Sep 08, 2010 3:18 pm

Schema API for Java?

Post by tobias.schoessler »

I am looking for a java based component /library / product which allows me to tell at a specific node of my XML document which operations ( add subnode, add attribute, delete node itself) are allowed based on a given XML schema.

Could anybody hint me into a direction which Java based XML API would allow me to implement this feature?

thank you
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Schema API for Java?

Post by adrian »

Hello,

Saxon should help with most of your needs. But I'm not sure if the freely available edition, Saxon-HE(Home Edition), covers all bases. Maybe you could use the old Saxon-B(has more features).

http://saxon.sourceforge.net/

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
tobias.schoessler
Posts: 4
Joined: Wed Sep 08, 2010 3:18 pm

Re: Schema API for Java?

Post by tobias.schoessler »

adrian, thanks alot for this quick reply.

wow! I am looking at there website right now http://www.saxonica.com , maybe I can offer them a redesign in exchange for a site license .. :)

I see XSLT and xQuery support for their main features, which is not exactly what I was looking for. Is there some specific feature in there product I should look for that would enable me to what I described in my post?

many thanks.
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Schema API for Java?

Post by adrian »

Saxon might be a bit overkill for what you need, but it also has rather good validation against XML schema. And that would be the main thing you will need to implement your feature.

I guess you could also Xerces instead: http://xerces.apache.org/xerces-j/

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
tobias.schoessler
Posts: 4
Joined: Wed Sep 08, 2010 3:18 pm

Re: Schema API for Java?

Post by tobias.schoessler »

I see. So you think that the Schema validation, XQuery, Xpath implementations are enough to implement a position aware xml editor api?

Maybe it is not clear what I want to do.

I have a xml document X which is conform to a schema Y.

I have a node in that document X. How can I find out which operations are allowed to manipulate the node, so that after the operation the XML document X is still conform with the schema Y?

e.g.
- which attributes are allowed to be added/removed/ ?
- which sub nodes can be added ?
- can the node be removed?
Radu
Posts: 9046
Joined: Fri Jul 09, 2004 5:18 pm

Re: Schema API for Java?

Post by Radu »

Hi Tobias,

It's very hard to do such a validation with a generic XML Schema.
This is no easy task, we've done something like this in our Author page to reject certain operations which might make the document not valid.

So first of all you would have to create a context of the XML node you are interested in (the path to the root element, find out the namespace the element is in).
Then, using the Xerces libraries (you will have to study the Xerces architecture for this) parse the XML Schema and obtain the SchemaGrammar objects from the GrammarPool and build an org.apache.xerces.impl.xs.XSModelImpl over them.

Then you will be able to query this model like:
public XSElementDeclaration getElementDeclaration(String name,
String namespace)


So you will have to map your XML element to a Xerces XSElementDeclaration and then look at this element declaration's type definition and see what is allowed and what is not allowed on the XML element.
There are a lot of complications with substitution groups and local-versus-global element declarations.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
tobias.schoessler
Posts: 4
Joined: Wed Sep 08, 2010 3:18 pm

Re: Schema API for Java?

Post by tobias.schoessler »

Radu, many thanks for your reply.

... and thank you for acknowledging this task to be difficult. I feel better already, as I am struggling with it. :)

So you do suggest using Xcerxes' object model of the Schema document to determine the possible opertions on a node?

I went down this route using this schema library

https://xsom.dev.java.net

which does seem to to do some of the work in abstracting grouping, global, local entity references for you by providing an abstract object model of the schema document.

Still deriving allowed operations does not seem to be trivial. I had exactly the problem you desribe, finding a schema entity object matching the node my cursor is at.

I do understand that oxygene is still able to do this? I am able to edit a document conforming an arbitrary XML schema and oxygene would support me in editing the document by guiding me which elements/attributes are allowed to be added/removed at my current cursor position?
Radu
Posts: 9046
Joined: Fri Jul 09, 2004 5:18 pm

Re: Schema API for Java?

Post by Radu »

Hi Tobias,

Oxygen uses Xerces to parse XML Schemas and get the internal object model representation. See this FAQ answer for starters:
http://xerces.apache.org/xerces2-j/faq-xs.html#faq-11
So I cannot give any advice about XOM because we haven't used it.

A lot of work has been put into this but Oxygen is a commercial application and out code sources are unavailable for public.
Maybe you can search for an open source XML editor and get some inspiration from it.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply