Page 1 of 1

check schema for fixed attribute

Posted: Mon Apr 13, 2015 4:11 pm
by LBarth
Hi,

I have attributes defined in a xsd schema declared as "fixed" on some elements like this :
<xs:element name="ABC">
<xs:complexType>
<xs:sequence>
<xs:element ref="Lib"/>
<xs:element ref="Content"/>
</xs:sequence>
<xs:attribute name="special" type="xs:boolean" fixed="true"/>
</xs:complexType>
</xs:element>

The xml instances do not carry the attribute :
<ABC>
<Lib>This is Lib</Lib>
<Content>This is Content</Content>
</ABC>

Is there a way to check with java if an element has a fixed attribute declared in the schema ?

Regards,
Lionel

Re: check schema for fixed attribute

Posted: Tue Apr 14, 2015 7:37 am
by Patrik
Hi Lionel,

you could load the file schema-aware. This way all the default and fixed attributes are expanded. But you won't be able to identify if an attribute was already set in the xml or has been added as default attribute.

Here's my (simplified) code to do this:

Code: Select all


import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import ro.sync.xml.parser.ParserCreator;
[...]
public static Document loadXmlFileSchemaAware(URL url) {
try {
final DocumentBuilder builder = ParserCreator.newSchemaAwareDocumentBuilder();
final Document document = builder.parse(url.getPath());
return document;
} catch(Exception e) {
return null;
}
}
Regards,
Patrik

Re: check schema for fixed attribute

Posted: Tue Apr 14, 2015 1:33 pm
by Radu
Hi,

Or if you have created a SAX parser to parse the XML documents, just set on it the property:

Code: Select all

parser.setFeature("http://apache.org/xml/features/validation/schema", true);
Regards,
Radu

Re: check schema for fixed attribute

Posted: Wed Apr 15, 2015 12:26 pm
by LBarth
Hi,

The point is the file is open in an Author view and it is schema aware.
I can see the fixed attribute in grey in the attributes panel of the Author view but when I try to access it with to following code I get a null value.

Code: Select all


if (fullySelectedNode.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
AuthorElement element = (AuthorElement) fullySelectedNode;
AttrValue attrValue = element.getAttribute("special");
}
If I try to parse the schema, I just parse one file, my schema include some sub-schemas (<xs:include schemaLocation="sub-schemas.xsd"/>) but the whole schema is already constructed inside oXygen as I can see in the attributes or elements panels and I can validate and make a lot of stuff in accordance with it, how can I check it ?

Regards,
Lionel

Re: check schema for fixed attribute

Posted: Wed Apr 15, 2015 1:11 pm
by Radu
Hi Lionel,

Could you double check? I tested a similar situation and it works for me.
I'm assuming that the XML document opened in the Author editing mode properly refers to the XML Schema using the usual xsi:schemaLocation attribute. Otherwise, Oxygen does not know what schema is associated to the XML document.

Regards,
Radu

Re: check schema for fixed attribute

Posted: Wed Apr 15, 2015 2:47 pm
by LBarth
Hi Radu,

Yes it works if I add a xsi:schemaLocation attribute in my xml file.

I do not understand how oXygen knows about the xml schema I declare in my framework in all panels and validation and so on and not in my java code.

Best regards,
Lionel

Re: check schema for fixed attribute

Posted: Wed Apr 15, 2015 3:03 pm
by Radu
Hi Lionel,

Right now, when parsing the XML document to obtain the AuthorElement nodes structure Oxygen needs the XML to have a direct reference to the schema. We'll try to see if we can remove this limitation in a future version.

Regards,
Radu

Re: check schema for fixed attribute

Posted: Tue Apr 28, 2015 5:01 pm
by Radu
Hi,

I forgot to ask if this is a showstopper for you. Oxygen uses the Xerces parser to parse the XML content and obtain the default attributes for each element while the content is parsed. And the Xerces parser loads and uses the XML Schema which is directly referenced by the XML document. So we would somehow need to redirect the parser to use an external schema while parsing, not impossible but also not very easy to do.

Regards,
Radu