Skip to main content

Universal CLI tool for editing SVG, HTML, and XML files using XPath and CSS selectors

Project description

๐Ÿ› ๏ธ XQR - XPath Query & Replace

Powerful CLI tool for editing SVG, HTML, and XML files using XPath and CSS selectors. Edit your structured documents directly from the command line or through a web interface.

๐ŸŽฏ What is XQR?

XQR (XPath Query & Replace) is a universal file editor that treats SVG, HTML, and XML as structured data containers. Use familiar XPath expressions and CSS selectors to query, modify, and manipulate content without specialized applications.

Perfect for:

  • Data Engineers - batch processing XML/SVG files
  • DevOps - configuration management and automation
  • Web Developers - HTML content manipulation
  • Designers - SVG batch editing and metadata management

๐Ÿš€ Features

  • Multiple File Formats: SVG, HTML, XML support with automatic format detection
  • XPath Queries: Full XPath 1.0 support for precise element selection
  • CSS Selectors: CSS selector support for HTML files
  • Multiple Interfaces: CLI commands, interactive shell, and web server
  • REST API: Programmatic access via HTTP endpoints
  • Batch Processing: Automate edits across multiple files
  • Backup System: Automatic backup creation before modifications

๐Ÿ“ฆ Installation

Using Poetry (Recommended)

# Clone the repository
git clone https://github.com/veridock/xqr.git
cd xqr

# Install with Poetry
poetry install

# Activate the environment
poetry shell

Using pip

pip install xqr

๐ŸŽฏ Quick Start

1. Create Example Files

xqr examples

2. Basic Usage

# Load and query a file
xqr load example.svg
xqr query "//text[@id='text1']"

# Update content
xqr set "//text[@id='text1']" "New Content"
xqr save

3. Interactive Shell

xqr shell
๐Ÿ“ > load example.html
๐Ÿ“ > query //title
๐Ÿ“ > set //title "Updated Title"
๐Ÿ“ > save
๐Ÿ“ > exit

4. Web Interface

xqr server --port 8080
# Open http://localhost:8080 in your browser

๐Ÿ“– Usage Examples

SVG Files - Update Charts & Graphics

# Update chart title
xqr set "//text[@id='title']" "Q4 Sales Results"

# Change visualization colors
xqr set "//rect[@id='bar1']" "blue" --type attribute --attr fill

# Update metadata for better organization
xqr set "//metadata/description" "Updated quarterly sales chart"

# Batch update multiple SVG files
for file in charts/*.svg; do
    xqr load "$file"
    xqr set "//metadata/updated" "$(date)"
    xqr save
done

HTML Files - Content Management

# Update page titles across multiple pages
xqr set "//title" "New Site Title"

# Change meta descriptions for SEO
xqr set "//meta[@name='description']" "Updated SEO description" --type attribute --attr content

# Update navigation links
xqr set "//nav//a[@href='/old-page']" "/new-page" --type attribute --attr href

# CSS selector support in shell mode
xqr shell
๐Ÿ“ > load index.html
๐Ÿ“ > query #main-heading
๐Ÿ“ > set #main-heading "Welcome to Our New Site"

XML Data Files - Configuration & Data

# Update configuration values
xqr set "//config/timeout" "60" --type attribute --attr value

# Modify data records
xqr set "//record[@id='1']/email" "newemail@example.com"

# Update version information
xqr set "//metadata/version" "2.0"

# Batch configuration updates
find /etc/configs -name "*.xml" -exec xqr load {} \; \
    -exec xqr set "//config/debug" "false" \; \
    -exec xqr save {} \;

๐Ÿ”ง Advanced Features

Batch Processing Scripts

#!/bin/bash
# Update copyright year across all HTML files
for file in **/*.html; do
    echo "Processing $file..."
    xqr load "$file"
    xqr set "//span[@class='copyright-year']" "2025"
    xqr save
done

# Update SVG chart data
#!/bin/bash
# Replace old data with new values
for chart in reports/*.svg; do
    xqr load "$chart"
    xqr set "//metadata/data-source" "Q1-2025-data.json"
    xqr set "//text[@class='last-updated']" "$(date '+%Y-%m-%d')"
    xqr save
done

REST API Integration

# Start server
xqr server --port 8080

# Load file via API
curl -X POST http://localhost:8080/api/load \
  -H "Content-Type: application/json" \
  -d '{"file_path": "dashboard.svg"}'

# Query elements
curl -X POST http://localhost:8080/api/query \
  -H "Content-Type: application/json" \
  -d '{"query": "//text[@class=\"metric-value\"]", "type": "xpath"}'

# Update values
curl -X POST http://localhost:8080/api/update \
  -H "Content-Type: application/json" \
  -d '{"xpath": "//text[@class=\"revenue\"]", "type": "text", "value": "$1.2M"}'

# Save changes
curl -X POST http://localhost:8080/api/save \
  -H "Content-Type: application/json" \
  -d '{"output_path": "updated_dashboard.svg"}'

XPath Examples

# Find elements by ID
//element[@id='myid']

# Find elements by attribute value
//rect[@fill='red']

# Find elements containing specific text
//text[contains(., 'Revenue')]

# Find elements by position
//record[position()=1]

# Find parent elements with specific children
//record[email='john@example.com']

# Complex queries with multiple conditions
//svg//text[@font-size='16' and contains(@class, 'title')]

CSS Selector Examples (HTML only)

# By ID
#main-title

# By class
.navigation-item

# By attribute
input[type='text']

# Descendant selectors
div.content p

# Pseudo-selectors
li:first-child

# Complex selectors
nav.primary ul.menu li a[href^="/products"]

๐Ÿ—๏ธ Project Structure

xqr/
โ”œโ”€โ”€ pyproject.toml          # Poetry configuration
โ”œโ”€โ”€ README.md              # This file
โ”œโ”€โ”€ Makefile               # Development automation
โ”œโ”€โ”€ xqr/                   # Main package
โ”‚   โ”œโ”€โ”€ __init__.py        # Package initialization
โ”‚   โ”œโ”€โ”€ core.py           # Core FileEditor class
โ”‚   โ”œโ”€โ”€ cli.py            # Command-line interface
โ”‚   โ”œโ”€โ”€ server.py         # HTTP server
โ”‚   โ””โ”€โ”€ examples.py       # Example file generator
โ””โ”€โ”€ tests/                # Test suite
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ test_core.py
    โ”œโ”€โ”€ test_cli.py
    โ””โ”€โ”€ test_server.py

๐Ÿงช Development

Setting up Development Environment

# Clone repository
git clone https://github.com/veridock/xqr.git
cd xqr

# Install with development dependencies
poetry install

# Create example files and run tests
make dev-setup

# Run full development cycle
make dev

Available Make Commands

make help           # Show all available commands
make install        # Install package
make test           # Run test suite
make test-cov       # Run tests with coverage
make format         # Format code with black
make lint           # Run linting
make examples       # Create example files
make demo-svg       # Run SVG demo
make run-server     # Start web server
make run-shell      # Start interactive shell

Running Tests

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov=xqr --cov-report=html

# Run specific test file
poetry run pytest tests/test_core.py -v

๐Ÿ“‹ Requirements

  • Python: 3.8+
  • lxml: For XPath support (automatically installed)
  • beautifulsoup4: For CSS selector support (automatically installed)

๐ŸŽฏ Use Cases

DevOps & Configuration Management

# Update configuration across multiple environments
for env in dev staging prod; do
    xqr load "config-${env}.xml"
    xqr set "//database/host" "db-${env}.company.com"
    xqr set "//cache/ttl" "3600"
    xqr save
done

Content Management

# Update copyright notices across all HTML files
find . -name "*.html" -exec xqr load {} \; \
    -exec xqr set "//footer//span[@class='year']" "2025" \; \
    -exec xqr save {} \;

Data Processing

# Extract and transform data from XML files
xqr shell << EOF
load sales-data.xml
list //record[sales>10000]
set //record[sales>10000]/status "high-performer"
save processed-sales.xml
exit
EOF

SVG Automation

# Update chart data and metadata
xqr load quarterly-chart.svg
xqr set "//text[@class='chart-title']" "Q1 2025 Results"
xqr set "//metadata/generated" "$(date)"
xqr setattr "//rect[@class='revenue-bar']" height "250"
xqr save

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/xpath-improvements)
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite (make test)
  6. Format your code (make format)
  7. Commit your changes (git commit -am 'Add XPath improvements')
  8. Push to the branch (git push origin feature/xpath-improvements)
  9. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ”— Links

๐ŸŒŸ Why XQR?

Traditional approaches require different tools for each format:

  • SVG files โ†’ Inkscape, Adobe Illustrator, manual editing
  • HTML files โ†’ Web browsers, text editors, sed/awk scripts
  • XML files โ†’ XML editors, custom parsers, XSLT

XQR provides a unified interface using standard web technologies:

  • XPath - W3C standard for XML/HTML navigation
  • CSS Selectors - Familiar syntax for web developers
  • Command Line - Scriptable and automation-friendly
  • REST API - Integration with existing workflows

Perfect for:

  • CI/CD pipelines - automated content updates
  • Content management - bulk HTML modifications
  • Data processing - XML transformation workflows
  • Design automation - SVG batch processing
  • Configuration management - XML config updates

Real-world Examples

E-commerce: Update product prices across thousands of XML files

find products/ -name "*.xml" -exec xqr set "//price[@currency='USD']" "$(calc_new_price {})" \;

Documentation: Update version numbers in all HTML docs

xqr set "//meta[@name='version']" "v2.1.0" --type attribute --attr content

Analytics: Update dashboard charts with new data

xqr set "//svg//text[@class='metric']" "$REVENUE_METRIC"

XQR - Making structured data editing simple, fast, and scriptable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xqr-0.1.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xqr-0.1.1-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file xqr-0.1.1.tar.gz.

File metadata

  • Download URL: xqr-0.1.1.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64

File hashes

Hashes for xqr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8f1d9ccf2ccba5601949519941d6e417bd960e7286ac423b422cf24a114222bf
MD5 4772fa64e51baebc8b041eaf6cfe48df
BLAKE2b-256 0f759e1a20e98ab416454764c53330343f654b949554840ed71d2f2e20bbc3b6

See more details on using hashes here.

File details

Details for the file xqr-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xqr-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64

File hashes

Hashes for xqr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d83513b19415a1d45d86a690a75a6672ec8b5c257702413b2c27b5c371d44432
MD5 afa91768cd500051a166986b02fcf62f
BLAKE2b-256 c653176a964fb1c12388469bad32dda6d40d520578d6539dd84b7d30fed1d4f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page