0

Below is the xml I am trying to sort using etree the value in fullName to be considered to sort alphabetically

<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
    <labels>
        <fullName>AnAutoQuote</fullName>
        <value>This is an automatically generated quote.</value>
        <language>en_US</language>
        <protected>false</protected>
        <shortDescription>Automatic Quote</shortDescription>
    </labels>
    <labels>
        <fullName>AManualQuote</fullName>
        <value>This is a manual quote.</value>
        <language>en_US</language>
        <protected>false</protected>
        <shortDescription>Manual Quote</shortDescription>
    </labels>
</CustomLabels>

Code that i tried but it returns the same xml as it got in the input. here xml contains the file that was read for input and parser is the xml parser with encoding set to utf-8


root = objectify.string(xml, parse)

for r in root.iter("labels"):
    r[:] = sorted(r, key = lambda ch: -(ch.tag=="fullname"))

print(etree.toStrin(root).decode("utf-8))

1 Answer 1

0
root = objectify.fromstring(xml)
root.labels = sorted(root.labels, key=lambda tag: tag.fullName)

full code example:

from lxml import objectify, etree

xml = '''<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
    <labels>
        <fullName>ZZZAManualQuote</fullName>
        <value>This is a manual quote.</value>
        <language>en_US</language>
        <protected>false</protected>
        <shortDescription>Manual Quote</shortDescription>
    </labels>
    <labels>
        <fullName>AnAutoQuote</fullName>
        <value>This is an automatically generated quote.</value>
        <language>en_US</language>
        <protected>false</protected>
        <shortDescription>Automatic Quote</shortDescription>
    </labels>
    <labels>
        <fullName>AManualQuote</fullName>
        <value>This is a manual quote.</value>
        <language>en_US</language>
        <protected>false</protected>
        <shortDescription>Manual Quote</shortDescription>
    </labels>
</CustomLabels>'''

root = objectify.fromstring(xml)
root.labels = sorted(root.labels, key=lambda tag: tag.fullName)

print(etree.tostring(root, pretty_print=True).decode('utf-8'))

output

<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
  <labels>
    <fullName>AManualQuote</fullName>
    <value>This is a manual quote.</value>
    <language>en_US</language>
    <protected>false</protected>
    <shortDescription>Manual Quote</shortDescription>
  </labels>
  <labels>
    <fullName>AnAutoQuote</fullName>
    <value>This is an automatically generated quote.</value>
    <language>en_US</language>
    <protected>false</protected>
    <shortDescription>Automatic Quote</shortDescription>
  </labels>
  <labels>
    <fullName>ZZZAManualQuote</fullName>
    <value>This is a manual quote.</value>
    <language>en_US</language>
    <protected>false</protected>
    <shortDescription>Manual Quote</shortDescription>
  </labels>
</CustomLabels>
Sign up to request clarification or add additional context in comments.

1 Comment

this gives an error called 'lxml.etree._Element' object has no attribute 'labels'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.