check schema for fixed attribute

Questions about XML that are not covered by the other forums should go here.
LBarth
Posts: 26
Joined: Mon Mar 04, 2013 1:42 pm

check schema for fixed attribute

Post 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
Patrik
Posts: 280
Joined: Thu Nov 28, 2013 9:32 am
Location: Hamburg/Germany
Contact:

Re: check schema for fixed attribute

Post 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
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: check schema for fixed attribute

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
LBarth
Posts: 26
Joined: Mon Mar 04, 2013 1:42 pm

Re: check schema for fixed attribute

Post 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
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: check schema for fixed attribute

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
LBarth
Posts: 26
Joined: Mon Mar 04, 2013 1:42 pm

Re: check schema for fixed attribute

Post 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
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: check schema for fixed attribute

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: check schema for fixed attribute

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply