A Python-based text processing tool that merges missing CONTROL structures between XML files while preserving original formatting and CDATA sections.
This script processes XML files as plain text and intelligently copies missing CONTROL elements from a source (ORIG) file into a target file, creating a complete output file. It's particularly useful for CIS benchmark migrations and policy structure management.
- Text-based Processing: Preserves original XML formatting and CDATA sections
- Smart Merging: Identifies and copies only missing CONTROL elements
- Position Preservation: Maintains the original position of CONTROL elements from the source file
- Auto-disable: Automatically sets
IS_CONTROL_DISABLEto 1 for copied controls - Section Tracking: Updates CONTROLS total count per section automatically
- Verbose Mode: Optional detailed reporting of inserted elements
- Python 3.6+
- No external dependencies (uses standard library only)
- Clone this repository:
git clone <repository-url>
cd CIS-Migration- Create a virtual environment (optional but recommended):
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepython xml_control_merger.py <ORIG_FILE> <TARGET_FILE> <OUT_FILE>python xml_control_merger.py --verbose <ORIG_FILE> <TARGET_FILE> <OUT_FILE>python xml_control_merger.py CIS_W2022.xml TEST_W2022.xml output.xmlWith detailed reporting:
python xml_control_merger.py --verbose CIS_W2022.xml TEST_W2022.xml output.xml- Load Files: Reads both ORIG and TARGET XML files as text
- Identify Sections: Extracts all SECTION numbers from the TARGET file
- Compare Controls: For each section, identifies missing CONTROL elements by comparing IDs
- Insert Missing Controls: Copies missing controls from ORIG to TARGET at their original positions
- Modify Attributes: Sets
IS_CONTROL_DISABLEto 1 for all copied controls - Update Counts: Updates the CONTROLS total attribute for each modified section
- Save Output: Writes the merged result to the output file
The script provides real-time feedback during processing:
- Section-by-section progress
- Number of missing controls found per section
- Insertion confirmation with position information
- Final summary (in verbose mode)
Loading ORIG file: CIS_W2022.xml
Loading TARGET file: TEST_W2022.xml
Creating 1:1 copy of the TARGET file as OUT file...
Performing CONTROL merge...
Processing section 1...
3 missing CONTROL elements found: {'12345', '12346', '12347'}
CONTROL 12345 was copied and inserted at position 0
CONTROL 12346 was copied and inserted at position 1
CONTROL 12347 was copied and inserted at position 2
CONTROLS total updated to 15
Processing completed successfully!
- CIS Benchmark policy migrations
- XML configuration file merging
- Policy structure synchronization
- Control element management across environments
This project is available for use in accordance with your organization's policies.
Contributions, issues, and feature requests are welcome.