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:elementname="Extensions"><xs:annotation><xs:documentation>Container for specific extensions.</xs:documentation></xs:annotation><xs:complexType><xs:sequence><xs:anynamespace="##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 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('<wildcardxmlns:n2="urn:issue14.2"><n2:e2>content</n2:e2></wildcard>').encode('utf-8'), x.toxml('utf-8', root_only=True))
# 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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.:
I tried something like the following:
In my output XML I am geting empty tag:
Ideally, it should populate the userdefined extensions like follows:
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.
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:
Also see the documentation for BindingDOMSupport which I think is supposed to be a similar API to to
xml.dom
.Hi Peter,
Thank you for the reply.
I tried something like this :
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?
I think you'd want to do:
to match what the example does.
Yeah tried that too! But not working
I am getting empty tag like : <ns2:Extensions/>. But in the object I can see the tag name and value being added.
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.