#116 Simple-typed elements accept any attributes

PyXB 1.1.4
closed
None
fixed
Content model
major
PyXB 1.1.3
defect
2012-01-20
2012-01-20
Yuri Khan
No

When an element is defined in the schema as having simple type, the resulting binding class accepts attributes on that element.

To reproduce

root.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" type="xsd:string"/>
</xsd:schema>

$ pyxbgen --version
pyxbgen from PyXB 1.1.3

$ pyxbgen -u root.xsd -m root
urn:uuid:13b5432e-436d-11e1-bbae-20cf303045b5
Python for AbsentNamespace0 requires 1 modules
Saved binding source to ./root.py

test.xml

<?xml version="1.0" encoding="utf-8"?>
<root foo='boo'/>

test.py

import root
root.CreateFromDocument(file('test.xml').read())

Expected behavior

Should throw an exception to the effect that foo is not an allowed attribute for root.

Observed behavior

No exception is thrown. I have not found any way to access the attribute value.

Other attempts at syntax

This doesn’t work either:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>
</xsd:element>
</xsd:schema>

Although this does:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
    <xsd:complexType>
        <xsd:simpleContent>
            <xsd:restriction base="xsd:string"/>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>
</xsd:schema>

$ python test.py
…
pyxb.exceptions_.UnrecognizedAttributeError: Attribute foo is not permitted in type None

Discussion

  • Peter A. Bigot

    Peter A. Bigot - 2012-01-20
    • status changed from new to closed
    • resolution set to fixed

    Thanks for the test case. Patch available in branch next. See related issue #117, which will be reviewed in preparation for 1.1.4 (whenever that may be).

    commit 382685fee22bf7e15b1823fc4c0b2d828eeef1b5
    Author: Peter A. Bigot <pabigot@…>
    Date: Fri Jan 20 11:13:05 2012 -0600

    trac/116: Simple-typed elements accept any attributes

    Move the instance attribute initialization code from complexTypeDefinition
    to _TypeBinding_mixin, so that the attempt to set attributes from a DOM
    source is made visible. Add support in simpleTypeDefinition to record the
    attempt to set an attribute: this raises the same exception as is raised in
    the complexTypeDefinition version when the attribute is not recognized.

    The comments in the code now imply that attribute settings are simply
    accepted, and a validation error occurs only when requested. As noted in
    trac/117 complex types have always raised exceptions immediately on an
    attempt to construct with an unrecognized attribute, regardless of
    validation settings. This may or may not be a bug (more thought is required
    on when the diagnostic for setting an attribute should be generated), but
    it's not related to the issue in trac/116, so it's being deferred.

     
  • Yuri Khan

    Yuri Khan - 2012-01-20

    Nice response time, thanks.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks