예제 #1
0
 def test_get_element_xpath_not_matching_element_without_namespace_prefix(
         self):
     xsd_string = "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'><xsd:element><xsd:complexType>" \
                  "</xsd:complexType></xsd:element></xsd:schema>"
     xpath = "element/complexType"
     xsd_tree = XSDTree.build_tree(xsd_string)
     namespaces = get_namespaces(xsd_string)
     with self.assertRaises(XMLError):
         get_element_by_xpath(xsd_tree, xpath, namespaces)
예제 #2
0
def _update_attribute(xsd_string, xpath, attribute, value=None):
    """Updates an attribute (sets the value or deletes)

    Args:
        xsd_string:
        xpath: xpath of the element to update
        attribute: name of the attribute to update
        value: value of the attribute to set

    Returns:

    """
    # Build the XSD tree
    xsd_tree = XSDTree.build_tree(xsd_string)
    # Get namespaces
    namespaces = get_namespaces(xsd_string)
    # Get XSD element using its xpath
    element = get_element_by_xpath(xsd_tree, xpath, namespaces)

    # Add or update the attribute
    if value is not None:
        # Set element attribute with value
        element.attrib[attribute] = value
    else:
        # Deletes attribute
        if attribute in element.attrib:
            del element.attrib[attribute]

    # Converts XSD tree back to string
    updated_xsd_string = XSDTree.tostring(xsd_tree)

    return updated_xsd_string
예제 #3
0
def _update_appinfo_element(xsd_string, xpath, appinfo_name, value=None):
    """Updates an appinfo element

    Args:
        xsd_string:
        xpath: xpath to element to update
        appinfo_name: name of the attribute to update
        value: value to set

    Returns:

    """
    # Build the XSD tree
    xsd_tree = XSDTree.build_tree(xsd_string)
    # Get namespaces
    namespaces = get_namespaces(xsd_string)
    # Get XSD element using its xpath
    element = get_element_by_xpath(xsd_tree, xpath, namespaces)

    if value is not None:
        # If a value is provided, create or update the appinfo
        add_appinfo_child_to_element(element, appinfo_name, value)
    else:
        # value is None, deletes the appinfo if present
        delete_appinfo_child_from_element(element, appinfo_name)

    # Converts XSD tree back to string
    updated_xsd_string = XSDTree.tostring(xsd_tree)

    return updated_xsd_string
예제 #4
0
 def test_get_element_xpath_matching_element_with_xs_namespace_prefix(self):
     xsd_string = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element><xs:complexType>" \
                  "</xs:complexType></xs:element></xs:schema>"
     xpath = "xs:element/xs:complexType"
     xsd_tree = XSDTree.build_tree(xsd_string)
     namespaces = get_namespaces(xsd_string)
     element = get_element_by_xpath(xsd_tree, xpath, namespaces)
     self.assertTrue(element is not None)
예제 #5
0
 def test_get_element_invalid_xpath(self):
     xsd_string = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><root><test></test></root></xs:schema>"
     xpath = "invalid"
     xsd_tree = XSDTree.build_tree(xsd_string)
     with self.assertRaises(XMLError):
         get_element_by_xpath(xsd_tree, xpath)
예제 #6
0
 def test_get_element_xpath_matching_element(self):
     xsd_string = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><root><test></test></root></xs:schema>"
     xpath = "root/test"
     xsd_tree = XSDTree.build_tree(xsd_string)
     element = get_element_by_xpath(xsd_tree, xpath)
     self.assertTrue(element is not None)