You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Chris Simmons <cp...@decisionsoft.com> on 2007/02/28 12:36:49 UTC

Default attributes according to schema grammar.

Hi all,

I'd like to be able to have attributes defaulted according to a schema 
grammar when I create a new element or at least when I add it to the 
DOM.  The only way I can see of doing this at the minute is to call 
Document.normalizeDocument() having configured it it to perform schema 
validation.  This seems like overkill when you consider that our 
documents contain literally thousands of nodes.

You can't do this manually through the DOM interfaces as there's no 
public setter corresponding with Attr.getSpecified() plus you'd have to 
do something very similar to the schema validator.

Is there a better way of doing this?

Regards,

Chris Simmons.

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Default attributes according to schema grammar.

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Chris Simmons <cp...@decisionsoft.com> wrote on 02/28/2007 06:36:49 AM:

> Hi all,
> 
> I'd like to be able to have attributes defaulted according to a schema 
> grammar when I create a new element or at least when I add it to the 
> DOM.  The only way I can see of doing this at the minute is to call 
> Document.normalizeDocument() having configured it it to perform schema 
> validation.  This seems like overkill when you consider that our 
> documents contain literally thousands of nodes.
> 
> You can't do this manually through the DOM interfaces as there's no 
> public setter corresponding with Attr.getSpecified() plus you'd have to 
> do something very similar to the schema validator.
> 
> Is there a better way of doing this?

You can probably get away with validating a subtree. You can do that with 
the JAXP 1.3 validation API [1].

Something like:

Element elem = ...;
SchemaFactory factory = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
...
Schema schema = factory.newSchema(...);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(elem), new DOMResult(elem));

This will augment the given element node and its descendants with default 
attributes, type info, etc...

> Regards,
> 
> Chris Simmons.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

[1] 
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/package-summary.html

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org