Exemple #1
0
    def visit_attribute(self, node, parent):
        """Declares an attribute.

            <attribute
              default = string
              fixed = string
              form = (qualified | unqualified)
              id = ID
              name = NCName
              ref = QName
              type = QName
              use = (optional | prohibited | required): optional
              {any attributes with non-schema Namespace...}>
            Content: (annotation?, (simpleType?))
            </attribute>
        """
        is_global = parent.tag == tags.schema

        # If the elment has a ref attribute then all other attributes cannot
        # be present. Short circuit that here.
        # Ref is prohibited on global elements (parent = schema)
        if not is_global:
            result = self.process_ref_attribute(node)
            if result:
                return result

        attribute_form = node.get('form', self.schema._attribute_form)
        qname = qname_attr(node, 'name', self.schema._target_namespace)
        if attribute_form == 'qualified' or is_global:
            name = qname
        else:
            name = etree.QName(node.get('name'))

        annotation, items = self._pop_annotation(node.getchildren())
        if items:
            xsd_type = self.visit_simple_type(items[0], node)
        else:
            node_type = qname_attr(node, 'type')
            if node_type:
                xsd_type = self._get_type(node_type)
            else:
                xsd_type = xsd_builtins.AnyType()

        # TODO: We ignore 'prohobited' for now
        required = node.get('use') == 'required'
        default = node.get('default')

        attr = xsd_elements.Attribute(name,
                                      type_=xsd_type,
                                      default=default,
                                      required=required)
        self.schema._elm_instances.append(attr)

        # Only register global elements
        if is_global:
            self.schema.register_attribute(qname, attr)
        return attr
Exemple #2
0
    def visit_attribute(self, node, parent):
        """Declares an attribute.

            <attribute
              default = string
              fixed = string
              form = (qualified | unqualified)
              id = ID
              name = NCName
              ref = QName
              type = QName
              use = (optional | prohibited | required): optional
              {any attributes with non-schema Namespace...}>
            Content: (annotation?, (simpleType?))
            </attribute>
        """
        attribute_form = node.get('form', self.schema._attribute_form)
        if attribute_form == 'qualified':
            name = qname_attr(node, 'name', self.schema._target_namespace)
        else:
            name = etree.QName(node.get('name'))

        xsd_type = None
        for child in node.iterchildren():
            if child.tag == tags.annotation:
                continue

            elif child.tag == tags.simpleType:
                assert xsd_type is None
                xsd_type = self.visit_simple_type(child, node)

        if xsd_type is None:
            node_type = qname_attr(node, 'type')
            try:
                xsd_type = self.schema.get_type(node_type)
            except KeyError:
                xsd_type = xsd_types.UnresolvedType(node_type)

        attr = xsd_elements.Attribute(name, type_=xsd_type)
        self.schema._elm_instances.append(attr)
        return attr
Exemple #3
0
    def visit_attribute(
        self, node: etree._Element, parent: etree._Element
    ) -> typing.Union[xsd_elements.Attribute, xsd_elements.RefAttribute]:
        """Declares an attribute.

        Definition::

            <attribute
              default = string
              fixed = string
              form = (qualified | unqualified)
              id = ID
              name = NCName
              ref = QName
              type = QName
              use = (optional | prohibited | required): optional
              {any attributes with non-schema Namespace...}>
            Content: (annotation?, (simpleType?))
            </attribute>

        :param node: The XML node
        :type node: lxml.etree._Element
        :param parent: The parent XML node
        :type parent: lxml.etree._Element

        """
        is_global = parent.tag == tags.schema

        # Check of wsdl:arayType
        array_type = node.get("{http://schemas.xmlsoap.org/wsdl/}arrayType")
        if array_type:
            match = re.match(r"([^\[]+)", array_type)
            if match:
                array_type = match.groups()[0]
                qname = as_qname(array_type, node.nsmap)
                array_type = UnresolvedType(qname, self.schema)

        # If the elment has a ref attribute then all other attributes cannot
        # be present. Short circuit that here.
        # Ref is prohibited on global elements (parent = schema)
        if not is_global:
            result = self.process_ref_attribute(node, array_type=array_type)
            if result:
                return result

        attribute_form = node.get("form", self.document._attribute_form)
        if attribute_form == "qualified" or is_global:
            name = qname_attr(node, "name", self.document._target_namespace)
        else:
            name = etree.QName(node.get("name"))

        annotation, items = self._pop_annotation(list(node))
        if items:
            xsd_type = self.visit_simple_type(items[0], node)
        else:
            node_type = qname_attr(node, "type")
            if node_type:
                xsd_type = self._get_type(node_type)
            else:
                xsd_type = xsd_types.AnyType()

        # TODO: We ignore 'prohobited' for now
        required = node.get("use") == "required"
        default = node.get("default")

        attr = xsd_elements.Attribute(name,
                                      type_=xsd_type,
                                      default=default,
                                      required=required)

        # Only register global elements
        if is_global:
            assert name is not None
            self.register_attribute(name, attr)
        return attr