check schema for fixed attribute

Questions about XML that are not covered by the other forums should go here.
LBarth
Posts: 25

check schema for fixed attribute

Mon Apr 13, 2015 4:11 pm

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: 227
Location: Hamburg/Germany

Re: check schema for fixed attribute

Tue Apr 14, 2015 7:37 am

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: 5376

Re: check schema for fixed attribute

Tue Apr 14, 2015 1:33 pm

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: 25

Re: check schema for fixed attribute

Wed Apr 15, 2015 12:26 pm

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: 5376

Re: check schema for fixed attribute

Wed Apr 15, 2015 1:11 pm

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: 25

Re: check schema for fixed attribute

Wed Apr 15, 2015 2:47 pm

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: 5376

Re: check schema for fixed attribute

Wed Apr 15, 2015 3:03 pm

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: 5376

Re: check schema for fixed attribute

Tue Apr 28, 2015 5:01 pm

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests