Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Namespace and Human Readable Paths #134

Open
natbprice opened this issue Jan 3, 2025 · 0 comments
Open

Default Namespace and Human Readable Paths #134

natbprice opened this issue Jan 3, 2025 · 0 comments

Comments

@natbprice
Copy link

Related to #52, the result returned by xmldiff is not human readable if there is a default namespace.

However, if we replace getpath() with getelementpath() then we can get a self-contained, human readable path. These paths work with the .find() method.

xpath = tree.getpath(element)

My current workaround is to strip the namespace before performing the diff operation, but this adds an extra step outside of the command line tool and may not be feasible in all use cases.

Would it be make sense to add the option for a path in the style of getelementpath()? If so, I would be willing to contribute to the development. Alternatively, would it make sense to add an option to strip namespaces?

from xmldiff import main, formatting, patch

left = """<HPXML xmlns="http://hpxmlonline.com/2023/09" schemaVersion="4.0">
  <XMLTransactionHeaderInformation>
    <XMLType>BPI-2101</XMLType>
    <XMLGeneratedBy>Noel</XMLGeneratedBy>
    <CreatedDateAndTime>2015-12-23T16:46:51.188445</CreatedDateAndTime>
    <Transaction>create</Transaction>
  </XMLTransactionHeaderInformation>
  <SoftwareInfo/>
  <Customer>
    <CustomerDetails>
      <Person>
        <SystemIdentifier id="person1"/>
        <Name>
          <FirstName>John</FirstName>
          <LastName>Doe</LastName>
        </Name>
      </Person>
    </CustomerDetails>
  </Customer>
</HPXML>
"""

right = """<HPXML xmlns="http://hpxmlonline.com/2023/09" schemaVersion="4.0">
  <XMLTransactionHeaderInformation>
    <XMLType>BPI-2101</XMLType>
    <XMLGeneratedBy>Noel</XMLGeneratedBy>
    <CreatedDateAndTime>2015-12-23T16:46:51.188445</CreatedDateAndTime>
    <Transaction>create</Transaction>
  </XMLTransactionHeaderInformation>
  <SoftwareInfo/>
  <Customer>
    <CustomerDetails>
      <Person>
        <SystemIdentifier id="person1"/>
        <Name>
          <FirstName>Jane</FirstName>
          <LastName>Doe</LastName>
        </Name>
      </Person>
    </CustomerDetails>
  </Customer>
</HPXML>
"""
diff = main.diff_texts(left=left, right=right, formatter=formatting.DiffFormatter())

print(f"Edit script: {diff}")

result = main.patch_text(diff, left)

print("\nPatched XML:")
print(result)

# Current Output:
# Edit script: [update-text, /*/*[3]/*/*/*[2]/*[1], "Jane"]

# With updates:
# utils.py line 115: Replace tree.getpath() with tree.getelementpath() 
# patch.py line 54: Replace tree.xpath() with tree.find()
# Edit script: [update-text, {http://hpxmlonline.com/2023/09}Customer/{http://hpxmlonline.com/2023/09}CustomerDetails/{http://hpxmlonline.com/2023/09}Person/{http://hpxmlonline.com/2023/09}Name/{http://hpxmlonline.com/2023/09}FirstName[1], "Jane"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant