This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # turn the root etree into a string | |
| ead_text = etree.tostring(tree, xml_declaration=True, encoding="utf-8") | |
| # write the string to your new EAD file | |
| # the directory path and filename can be whatever you'd like | |
| with open(os.path.join("path/to/ead/directory", "new_ead.xml"), mode="w") as f: | |
| f.write(ead_text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # accessing attribute values: | |
| >>> container.attrib.get("type", "") | |
| "box" | |
| >>> container.attrib.get("location", "") | |
| "" # since the "location" attribute does not exist, the .get() function returns an empty string | |
| # changing a current value or creating a new attribute | |
| >>> container.attrib["type"] = "folder" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> container = tree.xpath("//container")[0] | |
| >>> container.attrib | |
| {'type': 'box', 'label': 'Box'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # import the part of the library you'll need | |
| >>> from lxml.builder import E | |
| # the basic format for using E is: | |
| # E.[name of tag]([tag text], [attribute name]=[attribute value], [anything else that comes inside the tag]) | |
| # A single-tag example: | |
| >>> new_element = E.extent("25 photographs", encodinganalog="300") | |
| # printing to see the results |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # make a new, empty tag | |
| >>> new_tag = etree.Element("extent") | |
| # add some text | |
| >>> new_tag.text = "25 embarrassing photos" | |
| # add an attribute if you want any | |
| >>> new_tag.attrib["encodinganalog"] = "300" | |
| # insert the new tag into the master ead tree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> parent = extent.getparent() | |
| >>> print(etree.tostring(parent)) # printing out the parent just for comparison purposes | |
| ''' | |
| <physdesc altrender="whole"> | |
| <extent encodinganalog="300">3 linear feet and 1 oversize box</extent> | |
| </physdesc> | |
| ''' | |
| >>> parent.remove(extent) | |
| >>> print(etree.tostring(parent)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> extent.tag | |
| 'extent' | |
| >>> extent.tag = "physfacet" | |
| >>> extent.tag | |
| 'physfacet' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create a text representation of the element | |
| # etree.tostring() spits out all text, including tags and subtags | |
| text = etree.tostring(element_with_complex_text) | |
| # do the manipulation | |
| text = text.replace("Prince", "Artist formerly known as Prince") | |
| # transform that text back into an lxml element | |
| new_element = etree.fromstring(text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> extent.text | |
| '3 linear feet and 1 outsize box' | |
| # we don't use "outsize", so we'll make it "oversize" instead | |
| >>> extent.text = extent.text.replace(" outsize ", " oversize ") | |
| >>> extent.text | |
| '3 linear feet and 1 oversize box' |