Discussion:
[XOM-interest] Possible Bug: XOM Builder chokes on fixed optional attribute
Brian Uri!
2011-09-24 21:07:47 UTC
Permalink
Hello,

I seem to be hitting a bug, but I can't determine whether it's a bug in XOM
or just schemas which could be better written. I've pruned down the code and
schemas to the bare bits needed to reproduce the issue below.

I have a schema which defines an element and has a fixed optional attribute
from a third-party schema (XLink 1.1):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:buri:bugtest" xmlns:buri="urn:buri:bugtest"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="urn:buri:bugtest" elementFormDefault="qualified"
attributeFormDefault="qualified">

<xs:import namespace="http://www.w3.org/1999/xlink"
schemaLocation="XLink.xsd"/>

<xs:element name="revision" type="RevisionType" />
<xs:complexType name="RevisionType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xlink:type" fixed="resource"
use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>

The XLink 1.1 schema provides the definition for the xlink:type attribute:

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="http://www.w3.org/1999/xlink">

<xs:attribute name="type" type="xlink:typeType" />
<xs:simpleType name="typeType">
<xs:restriction base="xs:token">
<xs:enumeration value="simple"/>
<xs:enumeration value="extended"/>
<xs:enumeration value="title"/>
<xs:enumeration value="resource"/>
<xs:enumeration value="locator"/>
<xs:enumeration value="arc"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

When I try to build a Document with the instance below in a file, everything
works fine:

<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" xlink:type="resource" />

However, if I remove the attribute from the instance, like this:

<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" />

I get this error from XOM:

nu.xom.ParsingException: Unprefixed attribute type cannot be in default
namespace http://www.w3.org/1999/xlink
at nu.xom.Builder.build(Unknown Source)
[...]
Caused by: nu.xom.NamespaceConflictException: Unprefixed attribute type
cannot be in default namespace http://www.w3.org/1999/xlink
at nu.xom.Attribute._setNamespace(Unknown Source)
at nu.xom.Attribute.<init>(Unknown Source)
at nu.xom.NodeFactory.makeAttribute(Unknown Source)

If I understand it correctly, a fixed optional attribute need not exist in
an instance, but must have the fixed value if it does, so an instance
without it should still be valid. Since the 2nd instance doesn't even
mention "type", I'm not sure why XOM would be trying to build a type
attribute at all.

The environment details:
xom 1.2.6
xercesImpl 2.9.1
xml-apis 1.3.04

XMLReader: org.apache.xerces.parsers.SAXParser
http://xml.org/sax/features/validation=true
http://apache.org/xml/features/validation/schema=true
http://apache.org/xml/properties/schema/external-schemaLocationpoints
at the schema file

Test Code:
Builder builder = new Builder(xmlReader, true);
builder.build(new FileReader(new File(theInstance)));

Any insights would be appreciated. I'll be glad to provide more details if I
pruned too much!

Thanks!
BU
Michael Kay
2011-09-25 19:21:27 UTC
Permalink
The XSD 1.0 rules for fixed/default attributes of type QName are very
poorly defined, and it's best to avoid using them.

You code should work OK with an XSD 1.1 processor (I think), where the
rules have been more clearly spelled out.

Michael Kay
Saxonica
Post by Brian Uri!
Hello,
I seem to be hitting a bug, but I can't determine whether it's a bug in XOM
or just schemas which could be better written. I've pruned down the code and
schemas to the bare bits needed to reproduce the issue below.
I have a schema which defines an element and has a fixed optional attribute
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:buri:bugtest" xmlns:buri="urn:buri:bugtest"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="urn:buri:bugtest" elementFormDefault="qualified"
attributeFormDefault="qualified">
<xs:import namespace="http://www.w3.org/1999/xlink"
schemaLocation="XLink.xsd"/>
<xs:element name="revision" type="RevisionType" />
<xs:complexType name="RevisionType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xlink:type" fixed="resource"
use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="http://www.w3.org/1999/xlink">
<xs:attribute name="type" type="xlink:typeType" />
<xs:simpleType name="typeType">
<xs:restriction base="xs:token">
<xs:enumeration value="simple"/>
<xs:enumeration value="extended"/>
<xs:enumeration value="title"/>
<xs:enumeration value="resource"/>
<xs:enumeration value="locator"/>
<xs:enumeration value="arc"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
When I try to build a Document with the instance below in a file, everything
<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" xlink:type="resource" />
<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" />
nu.xom.ParsingException: Unprefixed attribute type cannot be in default
namespace http://www.w3.org/1999/xlink
at nu.xom.Builder.build(Unknown Source)
[...]
Caused by: nu.xom.NamespaceConflictException: Unprefixed attribute type
cannot be in default namespace http://www.w3.org/1999/xlink
at nu.xom.Attribute._setNamespace(Unknown Source)
at nu.xom.Attribute.<init>(Unknown Source)
at nu.xom.NodeFactory.makeAttribute(Unknown Source)
If I understand it correctly, a fixed optional attribute need not exist in
an instance, but must have the fixed value if it does, so an instance
without it should still be valid. Since the 2nd instance doesn't even
mention "type", I'm not sure why XOM would be trying to build a type
attribute at all.
xom 1.2.6
xercesImpl 2.9.1
xml-apis 1.3.04
XMLReader: org.apache.xerces.parsers.SAXParser
http://xml.org/sax/features/validation=true
http://apache.org/xml/features/validation/schema=true
http://apache.org/xml/properties/schema/external-schemaLocationpoints
at the schema file
Builder builder = new Builder(xmlReader, true);
builder.build(new FileReader(new File(theInstance)));
Any insights would be appreciated. I'll be glad to provide more details if I
pruned too much!
Thanks!
BU
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
Brian Uri!
2011-09-26 14:08:51 UTC
Permalink
Thanks for your reply, Michael!

I've found that XOM has this same behavior for any fixed optional
attributes, not just those referenced from another schema.

The workaround I am employing for now is to remove the fixed="resource" from
the attribute definition, and then adding a validation step in Java that
confirms the attribute is set to "resource" after XOM has loaded the
instance. This is sufficient for my single fringe case, but probably not
scalable.

Regards,
BU
Post by Michael Kay
The XSD 1.0 rules for fixed/default attributes of type QName are very
poorly defined, and it's best to avoid using them.
You code should work OK with an XSD 1.1 processor (I think), where the
rules have been more clearly spelled out.
Michael Kay
Saxonica
Post by Brian Uri!
Hello,
I seem to be hitting a bug, but I can't determine whether it's a bug in
XOM
Post by Brian Uri!
or just schemas which could be better written. I've pruned down the code
and
Post by Brian Uri!
schemas to the bare bits needed to reproduce the issue below.
I have a schema which defines an element and has a fixed optional
attribute
Post by Brian Uri!
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:buri:bugtest" xmlns:buri="urn:buri:bugtest"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="urn:buri:bugtest"
elementFormDefault="qualified"
Post by Brian Uri!
attributeFormDefault="qualified">
<xs:import namespace="http://www.w3.org/1999/xlink"
schemaLocation="XLink.xsd"/>
<xs:element name="revision" type="RevisionType" />
<xs:complexType name="RevisionType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xlink:type" fixed="resource"
use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
The XLink 1.1 schema provides the definition for the xlink:type
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
targetNamespace="http://www.w3.org/1999/xlink">
<xs:attribute name="type" type="xlink:typeType" />
<xs:simpleType name="typeType">
<xs:restriction base="xs:token">
<xs:enumeration value="simple"/>
<xs:enumeration value="extended"/>
<xs:enumeration value="title"/>
<xs:enumeration value="resource"/>
<xs:enumeration value="locator"/>
<xs:enumeration value="arc"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
When I try to build a Document with the instance below in a file,
everything
Post by Brian Uri!
<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" xlink:type="resource" />
<?xml version='1.0' encoding='UTF-8'?>
<buri:revision xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:buri="urn:buri:bugtest" />
nu.xom.ParsingException: Unprefixed attribute type cannot be in
default
Post by Brian Uri!
namespace http://www.w3.org/1999/xlink
at nu.xom.Builder.build(Unknown Source)
[...]
Caused by: nu.xom.NamespaceConflictException: Unprefixed attribute
type
Post by Brian Uri!
cannot be in default namespace http://www.w3.org/1999/xlink
at nu.xom.Attribute._setNamespace(Unknown Source)
at nu.xom.Attribute.<init>(Unknown Source)
at nu.xom.NodeFactory.makeAttribute(Unknown Source)
If I understand it correctly, a fixed optional attribute need not exist
in
Post by Brian Uri!
an instance, but must have the fixed value if it does, so an instance
without it should still be valid. Since the 2nd instance doesn't even
mention "type", I'm not sure why XOM would be trying to build a type
attribute at all.
xom 1.2.6
xercesImpl 2.9.1
xml-apis 1.3.04
XMLReader: org.apache.xerces.parsers.SAXParser
http://xml.org/sax/features/validation=true
http://apache.org/xml/features/validation/schema=true
http://apache.org/xml/properties/schema/external-schemaLocationpoints
Post by Brian Uri!
at the schema file
Builder builder = new Builder(xmlReader, true);
builder.build(new FileReader(new File(theInstance)));
Any insights would be appreciated. I'll be glad to provide more details
if I
Post by Brian Uri!
pruned too much!
Thanks!
BU
Elliotte Rusty Harold
2011-09-27 11:40:37 UTC
Permalink
I haven't had time to investigate your example in detail. However I do
know that XOM doesn't have any schema processing of its own. It's all
delegated to Xerces, so it's more likely a bug in the schema.

On the other hand, XOM has tighter namespace checking than Xerces,
does not support XML 1.1, and does not implement some backwards
incompatible changes the W3C made to namespaces a couple of years ago
so it's possible you're running up against some sort of mismatch.
Just maybe you're running into this, though it doesn't look quite
right for that.

More likely though is what Michael Kay suggested. The schema processor
isn't putting the attribute into the namespace you think it is, or
isn't properly reporting the correct namespace to XOM. He knows this
stuff better than I do.
--
Elliotte Rusty Harold
elharo at ibiblio.org
Loading...