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 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 LocateMatchingChildren (node, tag, namespace=pyxb.namespace.XMLSchema): """Locate all children of the DOM node that have a particular tag. This function returns a list of children of node which are ELEMENT_NODE instances and have a tag consistent with the given tag. @param node: An a xml.dom.Node ELEMENT_NODE instance. @param tag: the NCName of an element in the namespace, which defaults to the XMLSchema namespace. @keyword namespace: The namespace to which the child element belongs. Default is the XMLSchema namespace. @rtype: C{list(xml.dom.Node)} """ matches = [] for cn in node.childNodes: if (xml.dom.Node.ELEMENT_NODE == cn.nodeType) and namespace.nodeIsNamed(cn, tag): matches.append(cn) return matches