[Python] Need help comparing XSD files. Specifically, getting the paths/names of nodes in an...

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    So basically I need a script to compare an Original XSD file to an edited version of it to list all the changes.

    I used ChatGPT and it gave me a good result which pretty much does what I need.

    But the problem is in the output the way it writes out the nodes is not human readable.

    It gives something like /xs:schema/xs:complexType[50]/xs:attribute[1].

    Instead I would want it to give the names of each node in the path, like A/B/C.

    Each node has a name attribute so it should be possible but I’m not sure how.

    The code is below, if anyone can help it would be really appreciated!

    Code:

    from xmldiff import main
    from xmldiff.formatting import DiffFormatter
    from lxml import etree

    # Original Schema 1
    with open("XSD_Original_Version.xsd", "r", encoding="utf-8") as file1:
    schema1 = file1.read()
    # Modified Schema 2
    with open("XSD_SR_Version.xsd", "r", encoding="utf-8") as file2:
    schema2 = file2.read()

    # Parse the original schema into an ElementTree (needed by formatter)
    orig_tree = etree.fromstring(schema1)
    new_tree = etree.fromstring(schema2)

    # Compute the diff operations
    diff_ops = main.diff_trees(orig_tree, new_tree)

    # Format the changes into readable text
    formatter = DiffFormatter()
    formatted_diff = formatter.format(diff_ops, orig_tree)

    # Print the result
    print("Changes from Schema1 to Schema2:\n")
    for line in formatted_diff.splitlines():
    if line.strip():
    print(f"- {line}")



    Output:

    • [update-attribute, /xs:schema/xs:complexType[7]/xs:element[15], name, "Status"]
    • [update-attribute, /xs:schema/xs:complexType[7]/xs:element[15], type, "xs:string"]
    • [update-text-after, /xs:schema/xs:complexType[7]/xs:element[15], "\n\t\t"]
    • [move, /xs:schema/xs:complexType[6]/xs:choice[2]/xs:choice/xs:element[16], /xs:schema/xs:complexType[7], 15]
    • [update-attribute, /xs:schema/xs:complexType[7]/xs:element[16], name, "BusinessPhone"]
    • [update-text-after, /xs:schema/xs:complexType[7]/xs:element[16], "\n\t\t"]
    • [move, /xs:schema/xs:complexType[6]/xs:choice[2]/xs:choice/xs:element[75], /xs:schema/xs:complexType[7], 16]
    • [update-attribute, /xs:schema/xs:complexType[7]/xs:element[17], name, "GeocodeStatusDisplay"]
    • [update-text-after, /xs:schema/xs:complexType[7]/xs:element[17], "\n\t\t"]
    • .......

    Not sure what to put here

    Continue reading...

Compartilhe esta Página