Menu

toxml() uses incorrect name for root element

Help
gSalsero
2013-07-18
2013-07-18
  • gSalsero

    gSalsero - 2013-07-18

    Using this schema:

    xsd-with-hyphens.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               targetNamespace="urn:com:xsd:with:hyphens:xsd-with-hyphens"
               xmlns="urn:com:xsd:with:hyphens:xsd-with-hyphens"
               elementFormDefault="qualified"
               attributeFormDefault="unqualified"
               version="2012-06-06"
               xml:lang="en"
               xmlns:xsd-with-hyphens="urn:com:xsd:with:hyphens:xsd-with-hyphens"
               xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
               jxb:version="1.0"
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               jxb:extensionBindingPrefixes="xjc">
    
      <xs:annotation>
        <xs:appinfo>
          <jxb:globalBindings>
            <xjc:simple />
          </jxb:globalBindings>
        </xs:appinfo>
      </xs:annotation>
    
      <xs:complexType name="XsdWithHyphens">
        <xs:sequence>
            <xs:element name="username" type="Username">
              <xs:annotation>
                <xs:documentation>
                  The user's login name. Modification of this field is not allowed.  A domain and username combination uniquely identifies a user.
                </xs:documentation>
              </xs:annotation>
            </xs:element>
        </xs:sequence>
      </xs:complexType>
      <xs:simpleType name="Username">
        <xs:restriction base="t0">
          <xs:minLength value="1"/>
          <xs:maxLength value="256"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:element name="xsd-with-hyphens" type="XsdWithHyphens"/>
    
      <!-- locally generated simpleType helpers -->
    
      <xs:simpleType name="t0">
        <xs:restriction base="xs:string">
        <xs:pattern value="[^:]*"/>
        </xs:restriction>
      </xs:simpleType>
    
    </xs:schema>
    

    I created a python class using:

    pyxbgen -u xsd-with-hyphens.xsd -m XsdWithHyphensFactory
    

    Then I created this python test:

    import XsdWithHyphensFactory
    
    xsdHyphen = XsdWithHyphensFactory.XsdWithHyphens()
    xsdHyphen.username = "test"
    
    print xsdHyphen.toxml()
    

    I get the following xml:

    <?xml version="1.0"?>
    <ns1:XsdWithHyphens xmlns:ns1="urn:com:xsd:with:hyphens:xsd-with-hyphens">
      <ns1:username>test</ns1:username>
    </ns1:XsdWithHyphens>
    

    ns1:XsdWithHyphens is wrong. The xml should be this:

    <?xml version="1.0"?>
    <ns1:xsd-with-hyphens xmlns:ns1="urn:com:xsd:with:hyphens:xsd-with-hyphens">
      <ns1:username>test</ns1:username>
    </ns1:xsd-with-hyphens>
    

    I haven't been able to figure out how to force this other than supplying a parameter to the toxml() function which I'd rather not do.

    Any suggestions?

     
  • Peter A. Bigot

    Peter A. Bigot - 2013-07-18

    In your code:

    xsdHyphen = XsdWithHyphensFactory.XsdWithHyphens()
    

    you are not creating an instance of the xsd-with-hyphens element, you're creating an instance of the XsdWithHyphens complex type that is not associated with an element. Since toxml isn't told what element to use (and any number of elements might use that type), it emits the type name instead. Arguably, it should emit a warning about having a type that's not bound to an element. I'll add a tracker ticket for that.

    However, no patch to PyXB is needed to fix this. You could use:

    xsdHyphen = XsdWithHyphensFactory.XsdWithHyphens()
    xsdHyphen._setElement(XsdWithHyphensFactory.xsd_with_hyphens)
    xsdHyphen.username = "test"
    print xsdHyphen.toxml()
    

    to associate the element with the instance, but the preferred solution would be:

    xsdHyphen = XsdWithHyphensFactory.xsd_with_hyphens()
    xsdHyphen.username = "test"
    print xsdHyphen.toxml()
    

    which uses the Python "constructor" corresponding to the element (not its underlying type) to create the instance and associate the element with it. Note that pyxbgen renamed xsd-with-hyphens because it is not a valid Python identifier:

    WARNING:pyxb.binding.generate:Element {urn:com:xsd:with:hyphens:xsd-with-hyphens}xsd-with-hyphens renamed to xsd_with_hyphens
    
     
  • Peter A. Bigot

    Peter A. Bigot - 2013-07-18

    Also note that renaming the Python identifier had no effect on the generated XML; both alternatives generate the following:

    <?xml version="1.0" ?><ns1:xsd-with-hyphens xmlns:ns1="urn:com:xsd:with:hyphens:xsd-with-hyphens"><ns1:username>test</ns1:username></ns1:xsd-with-hyphens>
    
     
  • gSalsero

    gSalsero - 2013-07-18

    oh i see. My bad. I'm trying to convert from generateDS to PyXB. I didn't see anything in the documentation regarding this, though I'm sure I missed it. I'll have to go back and read it again slowly so I don't make these simple mistakes again.

    Thanks!

     

Log in to post a comment.

MongoDB Logo MongoDB