Extract specific XML elements from source files using Python and optionally format with xmllint. This skill should be used when users need to isolate a single XML element (like InstrumentVector with Id="0") from a larger XML document, preserving the complete element structure including opening and closing tags.
Extracts specific XML elements from source files using Python's standard library, with optional xmllint formatting for clean output. Use this when you need to isolate a single XML element (like `<InstrumentVector Id="0">`) from a larger document while preserving its complete structure including opening and closing tags.
/plugin marketplace add krfantasy/alsdiff/plugin install xml-element-extractor@alsdiff-toolsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
reference.mdscripts/extract_xml_element.pyThis skill enables precise extraction of XML elements from source files using Python's standard library (xml.etree.ElementTree), with optional xmllint formatting for clean output. Use this skill when you need to isolate a specific XML element from a larger document while maintaining its complete structure.
DO NOT READ XML FILES DIRECTLY
This prevents context overflow and maintains performance.
Key advantages of the Python implementation:
To extract an XML element:
Gather the following parameters:
<InstrumentVector Id="0">)Run the extraction script with the three parameters:
python3 scripts/extract_xml_element.py <source_file> <dest_file> <element_tag>
Example:
python3 scripts/extract_xml_element.py source.xml dest.xml '<InstrumentVector Id="0">'
Note: The script is implemented in Python but maintains the .py extension for compatibility with existing workflows. The shebang line ensures it executes with Python.
The script will:
The script provides basic error handling for common issues:
If xmllint is available on the system, the script will automatically format the extracted XML with:
If xmllint is not available, the extracted element will be saved in its original formatting.
# Extract InstrumentVector with Id="0"
python3 scripts/extract_xml_element.py live_set.xml instrument_vector.xml '<InstrumentVector Id="0">'
# Extract MidiTrack element
python3 scripts/extract_xml_element.py tracks.xml midi_track.xml '<MidiTrack Id="42">'
# Extract complex elements with quotes and special characters
python3 scripts/extract_xml_element.py live_set.xml device.xml '<MxDeviceAudioEffect Id="5">'
# This will fail - source file doesn't exist
python3 scripts/extract_xml_element.py missing.xml output.xml '<Element>'
# This will fail - no matching element found
python3 scripts/extract_xml_element.py source.xml output.xml '<NonExistentTag>'
# This will fail - malformed XML
python3 scripts/extract_xml_element.py malformed.xml output.xml '<Element>'
extract_xml_element.py: Main Python script that performs XML element extraction using Python's standard library. The script:
python_xml.md: Reference documentation for XML element extraction process, including explanations of the Python parsing logic and troubleshooting guides for common extraction issues.
The script uses Python's standard library with the following key components:
XML Parsing:
xml.etree.ElementTree.parse(): Parses XML files into structured element treestree.iter(tag_name): Iterates through all elements with matching tag namesET.tostring(): Converts elements back to XML stringsTag Matching Algorithm:
Error Detection:
ET.ParseErrorFormatting Options:
XMLLINT_INDENT environment variable for 2-space indentationxml.dom.minidom for cross-platform compatibilityThe Python implementation provides consistent behavior across:
No platform-specific tools are required, making the skill truly portable.