def LocateUniqueChild(node, tag, absent_ok=True, namespace=pyxb.namespace.XMLSchema): """Locate a unique child of the DOM node. This function returns the sole child of node which is an ELEMENT_NODE instance and has a tag consistent with the given tag. If multiple nodes with a matching C{tag} are found, or C{absent_ok} is C{False} and no matching tag is found, an exception is raised. @param node: An a xml.dom.Node ELEMENT_NODE instance @param tag: the NCName of an element in the namespace @keyword absent_ok: If C{True} (default), C{None} is returned if no match can be found. If C{False}, an exception is raised if no match can be found. @keyword namespace: The namespace to which the child element belongs. Default is the XMLSchema namespace. @rtype: C{xml.dom.Node} @raise pyxb.SchemaValidationError: multiple elements are identified @raise pyxb.SchemaValidationError: C{absent_ok} is C{False} and no element is identified. """ candidate = None for cn in node.childNodes: if (xml.dom.Node.ELEMENT_NODE == cn.nodeType) and namespace.nodeIsNamed(cn, tag): if candidate: raise pyxb.SchemaValidationError( 'Multiple %s elements nested in %s' % (tag, node.nodeName)) candidate = cn if (candidate is None) and not absent_ok: raise pyxb.SchemaValidationError('Expected %s elements nested in %s' % (tag, node.nodeName)) return candidate
def interpretQName(self, name, namespace=None): """Convert the provided name into an L{ExpandedName}, i.e. a tuple of L{Namespace} and local name. If the name includes a prefix, that prefix must map to an in-scope namespace in this context. Absence of a prefix maps to L{defaultNamespace()}, which must be provided (or defaults to the target namespace, if that is absent). @param name: A QName. @type name: C{str} or C{unicode} @param name: Optional namespace to use for unqualified names when there is no default namespace. Note that a defined default namespace, even if absent, supersedes this value. @return: An L{ExpandedName} tuple: ( L{Namespace}, C{str} ) @raise pyxb.SchemaValidationError: The prefix is not in scope @raise pyxb.SchemaValidationError: No prefix is given and the default namespace is absent """ assert isinstance(name, (str, unicode)) if 0 <= name.find(':'): (prefix, local_name) = name.split(':', 1) assert self.inScopeNamespaces() is not None namespace = self.inScopeNamespaces().get(prefix) if namespace is None: raise pyxb.SchemaValidationError( 'No namespace declared for QName %s prefix' % (name, )) else: local_name = name # Context default supersedes caller-provided namespace if self.defaultNamespace() is not None: namespace = self.defaultNamespace() # If there's no default namespace, but there is a fallback # namespace, use that instead. if (namespace is None) and self.__fallbackToTargetNamespace: namespace = self.targetNamespace() if namespace is None: raise pyxb.SchemaValidationError( 'QName %s with absent default namespace cannot be resolved' % (local_name, )) # Anything we're going to look stuff up in requires a component model. # Make sure we can load one, unless we're looking up in the thing # we're constructing (in which case it's being built right now). if (namespace != self.targetNamespace()): namespace.validateComponentModel() return pyxb.namespace.ExpandedName(namespace, local_name)
def LocateFirstChildElement(node, absent_ok=True, require_unique=False, ignore_annotations=True): """Locate the first element child of the node. @param node: An a xml.dom.Node ELEMENT_NODE instance. @keyword absent_ok: If C{True} (default), C{None} is returned if no match can be found. If C{False}, an exception is raised if no match can be found. @keyword require_unique: If C{False} (default), it is acceptable for there to be multiple child elements. If C{True}, presence of multiple child elements raises an exception. @keyword ignore_annotations: If C{True} (default), annotations are skipped wheen looking for the first child element. If C{False}, an annotation counts as an element. @rtype: C{xml.dom.Node} @raise SchemaValidationError: C{absent_ok} is C{False} and no child element was identified. @raise SchemaValidationError: C{require_unique} is C{True} and multiple child elements were identified """ candidate = None for cn in node.childNodes: if xml.dom.Node.ELEMENT_NODE == cn.nodeType: if ignore_annotations and pyxb.namespace.XMLSchema.nodeIsNamed( cn, 'annotation'): continue if require_unique: if candidate: raise pyxb.SchemaValidationError( 'Multiple elements nested in %s' % (node.nodeName, )) candidate = cn else: return cn if (candidate is None) and not absent_ok: raise pyxb.SchemaValidationError('No elements nested in %s' % (node.nodeName, )) return candidate
def updateFromDOM(self, node): if not node.hasAttribute('name'): raise pyxb.SchemaValidationError('No name attribute in facet') assert node.getAttribute('name') == self.Name() self._updateFromDOM(node)