Menu

Add user defined tags

Help
Lavanya
2018-07-05
2018-07-05
  • Lavanya

    Lavanya - 2018-07-05

    Hi All,

    I am trying to add userdefined tags to my schema. The schema is defined as I can add any element to a particular node.:

    <xs:element name="Extensions">
            <xs:annotation>
                <xs:documentation>Container for specific extensions.</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                <xs:sequence>
                    <xs:any namespace="##any" processContents="lax" maxOccurs="unbounded">
                        <xs:annotation>
                            <xs:documentation>Accepts any element(s) the content provider wants to put here.</xs:documentation>
                        </xs:annotation>
                    </xs:any>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    

    I tried something like the following:

        reg.Extensions._complexTypeDefinition__wildcardElements.append(pyxb.utils.saxdom.Element)
            reg.Extensions.wildcardElements()[0].nodeName = "name"
            reg.Extensions.wildcardElements()[0].value = "dummy"
    

    In my output XML I am geting empty tag:

    <ns1:Extensions/>
    

    Ideally, it should populate the userdefined extensions like follows:

    <ns1:Extensions>
                <ns2:name>dummy</ns2:name>
    </ns1:Extensions>
    

    Note: I alos need to add ns2 defined by user.

    Can you please help me as to which methods are missing here to bind the element to my xml.

     
  • Peter A. Bigot

    Peter A. Bigot - 2018-07-05

    I think you're adding the class pyxb.utils.saxdom.Element, rather than an instance of a DOM element. So what's there isn't recognized by PyXB.

    test-issue-0014 has an example of what you're trying to do. See that test case for full context, but you'll want something like this:

    # Create a namespace and tell PyXB what to call it in generated documents
    ns2 = pyxb.namespace.Namespace("urn:issue14.2")
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(ns2, 'n2')
    
    # Construct a DOM element for tag e2 in the ns2 namespace and give it
    some content.
    bds = pyxb.utils.domutils.BindingDOMSupport()
    elt = bds.createChildElement(ns2.createExpandedName('e2'))
    bds.appendTextChild('content', elt)
    
    # Create an element that accepts wildcard content.
    x = wildcard()
    self.assertEqual(six.u('<wildcard/>').encode('utf-8'), x.toxml('utf-8', root_only=True))
    
    # Add the wildcard content to it
    x._appendWildcardElement(elt)
    self.assertEqual(six.u('<wildcard xmlns:n2="urn:issue14.2"><n2:e2>content</n2:e2></wildcard>').encode('utf-8'), x.toxml('utf-8', root_only=True))
    

    Also see the documentation for BindingDOMSupport which I think is supposed to be a similar API to to xml.dom.

     
  • Lavanya

    Lavanya - 2018-07-05

    Hi Peter,

    Thank you for the reply.

    I tried something like this :

     # Construct a DOM element for tag e2 in the ns2 namespace and give it some content.
        bds = pyxb.utils.domutils.BindingDOMSupport()
        name = bds.createChildElement(ns2.createExpandedName('name'))
        bds.appendTextChild("dummy", name)
    
        reg.Extensions = pyxb.BIND()
        reg.Extensions._complexTypeDefinition__wildcardElements.append(name)
    

    When I did reg.Extensions.wildcardElements()[0].toprettyxml() I can print the XML content. But it is not printing in my output xml file.

    Is there somethinf wrong I have done in generating?

     
  • Peter A. Bigot

    Peter A. Bigot - 2018-07-05

    I think you'd want to do:

    reg.Extensions._appendWildcardElement(name)
    

    to match what the example does.

     
  • Lavanya

    Lavanya - 2018-07-05

    Yeah tried that too! But not working

     
  • Lavanya

    Lavanya - 2018-07-05

    I am getting empty tag like : <ns2:Extensions/>. But in the object I can see the tag name and value being added.

     
  • Peter A. Bigot

    Peter A. Bigot - 2018-07-05

    I'm not getting enough information to tell what the problem is, probably because it depends on your schema. Take the test-issue-0014 example which is self-contained, and change it incrementally until it does what you're trying to do. That should either reveal the problem, or at least provide something you can provide in an issue on github where I could see exactly what's going on.

     

Log in to post a comment.