Qualified Attribute namespace prefixes disappear when a default namespace is used

Bart Kraan
  • Bart Kraan

    Bart Kraan - 2013-07-10

    Hello Pyxb team,

    I have posted this question on the help forum two times in the last weeks, but I probably do it the wrong way. The post is placed on the bottom of the list and states ‘post awaiting moderation’.

    I am trying to generate valid XML with Pyxb, by instantiating new objects and filling them with data form a database.
    When I set the namespace of a schema to default, the generated XML is not valid.
    I get the following error:
    ERROR: Element '{http://sample}ValueAmount', attribute 'currencyID': The attribute 'currencyID' is not allowed.

    I have found that the reason for this error is the fact that the attributes in the XML are not prefixed with the namespaceprefix.
    Is there a way I can get Pyxb to keep the namespaceprefixes on attributes when I set the namespace to default?
    Or is this an error?

    The project I am working on has an XML schema with qualified elements and qualified attributes.

    This is an example of the XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://sample" targetNamespace="http://sample" elementFormDefault="qualified" attributeFormDefault="qualified">
        <xs:element name="samplerootelement" type="samplerootelementType"/>
        <xs:complexType name="samplerootelementType">
                <xs:element name="sampleelement" type="sampleelementType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:complexType name="sampleelementType">
                <xs:element name="ValueAmount" type="AmountType" minOccurs="0"/>
        <xs:complexType name="AmountType">
                <xs:extension base="xs:decimal">
                    <xs:attribute name="currencyID" type="xs:normalizedString" use="optional"/>

    The generation of XML is done with:

    import pyxb
    import sample
    elm = sample.sampleelementType()
    elm.ValueAmount = '100'
    elm.ValueAmount.currencyID = 'abc'
    sam = sample.samplerootelement()
    from pyxb.namespace.builtin import XMLSchema_instance as xsi
    bds = pyxb.utils.domutils.BindingDOMSupport()
    samdom = sam.toDOM(bds)
    bds.addAttribute(samdom.documentElement, xsi.createExpandedName('schemaLocation'), "c:\sample.xsd")
    print samdom.toprettyxml(encoding = "utf-8")

    This python program gives the following XML:

    <?xml version="1.0" encoding="utf-8"?>
    <samplerootelement xmlns="http://sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="c:\sample.xsd">
            <ValueAmount currencyID="abc">100.0</ValueAmount>

    When I validate this XML, I get the error above. This is because the currencyID attribute has no prefix, while it is a qualified attribute.
    The W3C reccomendation on Namespases: http://www.w3.org/TR/REC-xml-names/#scoping-defaulting gives the following explanation:
    6.2 Namespace Defaulting
    The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations. In the case of an empty tag, the scope is the tag itself.
    A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
    If there is a default namespace declaration in scope, the expanded name corresponding to an unprefixed element name has the URI of the default namespace as its namespace name. If there is no default namespace declaration in scope, the namespace name has no value. The namespace name for an unprefixed attribute name always has no value. In all cases, the local name is local part (which is of course the same as the unprefixed name itself).

    When the namespace is not set to default, every element and attribute in the generated XML is prefixed correctly with the namespaceprefix. The XML is valid.

    <?xml version="1.0" encoding="utf-8"?>
    <ns1:samplerootelement xmlns:ns1="http://sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="d:\redex\sample.xsd">
            <ns1:ValueAmount ns1:currencyID="abc">100.0</ns1:ValueAmount>

    The XML output needed for my project is this:

    <?xml version="1.0" encoding="utf-8"?>
    <samplerootelement xmlns="http://sample" xmlns:ns1="http://sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="c:\sample.xsd">
            <ValueAmount ns1:currencyID="abc">100.0</ValueAmount>

    Is there a way to get this XML using Pyxb?

  • Peter A. Bigot

    Peter A. Bigot - 2013-07-10

    This is a bug. You can get the XML you desire if you apply the patches that fix issue #202.

    Thanks for reporting the problem.

    Last edit: Peter A. Bigot 2013-07-10
  • Peter A. Bigot

    Peter A. Bigot - 2013-07-10

    I was unaware that SourceForge had added moderation to these forums, and though I see your postings in a moderation queue I can't actually see their content, so don't know why they were held (or what good a moderation feature might be). I've at least added my email address as a notification address so this shouldn't happen again.

  • Bart Kraan

    Bart Kraan - 2013-07-12

    Hello Peter,

    After applying the patch, it works all perfect.
    Thank you for the quick response.



Log in to post a comment.

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

Sign up for the SourceForge newsletter:

No, thanks