This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate: replace incorrect use of default with fallback
Commit 68ca552 introduced an error in nomenclature by confusing the concept of a default namespace with the concept of a fallback namespace. The two are not at all the same thing. A fallback namespace is used to associate an element or attribute identified by an expanded name that has no namespace name with the information set from a schema that had no target namespace. The feature exists because there is no other way to perform that association. A default namespace is used to provide a default namespace name if there is no namespace prefix on the tag. In valid XML this is specified through an `xmlns=uri` attribute in a start tag. To this point PyXB has not accepted as valid any XML where the root element was in a non-absent namespace that was not explicitly identified. PyXB depends on the XML parser to determine the namespace name of an expanded name. It is only when that name is absent (and so could not have been provided by valid XML) that PyXB will associate the corresponding information set from a contextually-specified absent Namespace. It is outside PyXB's scope to override namespaces in a situation where valid XML requires a namespace name and the parser fails to provide one. Since xml.sax does not appear to provide a mechanism to assign an initial default namespace that is not present in the XML text there is no way to support the use case in issue #94 within PyXB. This commit makes no intentional behavioral change, but to reduce future confusion it changes the canonical keyword and positional argument to be `fallback_namespace`. It adds a temporary workaround to accept `default_namespace` as a deprecated alias in case somebody's depending on the existing behavior. Closes #94.
- Loading branch information
Showing
3 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
import logging | ||
import pyxb.binding.generate | ||
import pyxb.utils.domutils | ||
import xml.dom.minidom as dom | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig() | ||
_log = logging.getLogger(__name__) | ||
|
||
xsd = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" | ||
targetNamespace="urn:test-issue-0094" | ||
xmlns:tns="urn:test-issue-0094"> | ||
<xs:element name="MARKETPLACE"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="NAME" type="xs:string"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
<xs:element name="MARKETPLACE_POOL"> | ||
<xs:complexType> | ||
<xs:sequence maxOccurs="1" minOccurs="1"> | ||
<xs:element ref="tns:MARKETPLACE" maxOccurs="unbounded" minOccurs="0"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema> | ||
''' | ||
|
||
# Fully qualified XML | ||
fqXmlSample = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ns:MARKETPLACE_POOL xmlns:ns="urn:test-issue-0094"> | ||
<ns:MARKETPLACE> | ||
<ns:NAME>OpenNebula Public</ns:NAME> | ||
</ns:MARKETPLACE> | ||
</ns:MARKETPLACE_POOL> | ||
''' | ||
|
||
# Default-qualified XML | ||
dqXmlSample = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<MARKETPLACE_POOL xmlns="urn:test-issue-0094"> | ||
<MARKETPLACE> | ||
<NAME>OpenNebula Public</NAME> | ||
</MARKETPLACE> | ||
</MARKETPLACE_POOL> | ||
''' | ||
|
||
# XML that lacks any namespace association | ||
nqXmlSample = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<MARKETPLACE_POOL> | ||
<MARKETPLACE> | ||
<NAME>OpenNebula Public</NAME> | ||
</MARKETPLACE> | ||
</MARKETPLACE_POOL> | ||
''' | ||
|
||
code = pyxb.binding.generate.GeneratePython(schema_text=xsd) | ||
#open('code.py', 'w').write(code) | ||
rv = compile(code, 'test', 'exec') | ||
eval(rv) | ||
|
||
import unittest | ||
|
||
class TestIssue0094 (unittest.TestCase): | ||
def testFullyQualified (self): | ||
doc = CreateFromDocument(fqXmlSample); | ||
self.assertEqual(doc.MARKETPLACE[0].NAME, "OpenNebula Public") | ||
|
||
def testDefaultQualified (self): | ||
doc = CreateFromDocument(dqXmlSample); | ||
self.assertEqual(doc.MARKETPLACE[0].NAME, "OpenNebula Public") | ||
|
||
def testNoQualifier (self): | ||
with self.assertRaises(pyxb.UnrecognizedDOMRootNodeError) as cm: | ||
doc = CreateFromDocument(nqXmlSample) | ||
|
||
def testNoQualifierDefaulted (self): | ||
with self.assertRaises(pyxb.UnrecognizedDOMRootNodeError) as cm: | ||
doc = CreateFromDocument(nqXmlSample, default_namespace=Namespace) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters