This directory contains practical examples demonstrating how to use xml-lib programmatically in your Python projects.
xml-lib provides a clean, well-documented Python API for XML validation, linting, and publishing. These examples show real-world usage patterns that go beyond simple CLI usage.
# Install xml-lib
pip install xml-lib
# Or install from source in development mode
pip install -e .What it demonstrates:
- Quick validation with sensible defaults
- Error and warning handling
- Progress indicators
- Accessing validation metadata (checksums, timestamps)
When to use:
- Development scripts
- Pre-commit hooks
- Simple CI/CD checks
- Learning the basics
Run it:
python examples/programmatic/01_basic_validation.pyKey patterns:
from xml_lib import quick_validate
# Simplest usage - automatic discovery
result = quick_validate("path/to/project")
if result.is_valid:
print(f"✓ Validated {len(result.validated_files)} files")
else:
for error in result.errors:
print(f"✗ {error.file}:{error.line} - {error.message}")What it demonstrates:
- Validating multiple projects efficiently
- Reusing validator instances for performance
- Generating validation reports
- Exit code handling for CI/CD
When to use:
- CI/CD pipelines with multiple repos
- Nightly validation jobs
- Monorepo validation
- Quality assurance workflows
Run it:
python examples/programmatic/02_batch_processing.pyKey patterns:
from xml_lib import create_validator
# Create once, reuse many times
validator = create_validator(
schemas_dir="schemas",
guardrails_dir="lib/guardrails",
enable_streaming=True,
)
# Validate multiple projects
for project in projects:
result = validator.validate_project(project)
print(f"{project}: {'✓' if result.is_valid else '✗'}")What it demonstrates:
- Multi-stage validation pipeline (lint → validate → artifacts → report)
- Conditional logic based on validation results
- Strict mode (treating warnings as errors)
- Integration with existing tooling
When to use:
- Documentation build pipelines
- Custom quality gates
- Integration with other tools
- Complex validation workflows
Run it:
python examples/programmatic/03_custom_workflow.pyKey patterns:
from xml_lib import lint_xml, validate_xml
# Stage 1: Lint for issues
lint_result = lint_xml("project", check_external_entities=True)
if lint_result.has_errors:
print("Linting failed!")
return
# Stage 2: Validate against schemas
validation_result = validate_xml("project", enable_streaming=True)
if not validation_result.is_valid:
print("Validation failed!")
return
# Stage 3: Generate artifacts (only if validation passed)
generate_artifacts()from xml_lib import quick_validate
result = quick_validate("my-project")
if result.is_valid:
print("✓ All files valid!")from xml_lib import validate_xml
from xml_lib.sanitize import MathPolicy
result = validate_xml(
"my-project",
schemas_dir="custom/schemas",
guardrails_dir="custom/guardrails",
math_policy=MathPolicy.SANITIZE,
enable_streaming=True,
streaming_threshold_mb=50,
show_progress=True,
)from xml_lib import create_validator
validator = create_validator(
schemas_dir="schemas",
guardrails_dir="lib/guardrails",
)
# Use for multiple projects
for project in ["project1", "project2", "project3"]:
result = validator.validate_project(Path(project))
print(f"{project}: {'✓' if result.is_valid else '✗'}")from xml_lib import lint_xml
result = lint_xml(
"project",
check_indentation=True,
check_external_entities=True,
indent_size=2,
)
print(f"Errors: {result.error_count}")
print(f"Warnings: {result.warning_count}")
for issue in result.issues:
print(issue.format_text())For complete API documentation, use Python's built-in help:
import xml_lib
help(xml_lib) # Package overview
help(xml_lib.quick_validate) # Function documentation
help(xml_lib.Validator) # Class documentationOr access docstrings directly:
from xml_lib import validate_xml
print(validate_xml.__doc__)#!/usr/bin/env python3
from pathlib import Path
from xml_lib import quick_validate
import sys
result = quick_validate(Path.cwd())
if not result.is_valid:
print("✗ XML validation failed")
for error in result.errors:
print(f" {error.file}:{error.line} - {error.message}")
sys.exit(1)
print("✓ All XML files valid")
sys.exit(0)- name: Validate XML
run: |
pip install xml-lib
python examples/programmatic/01_basic_validation.pyimport pytest
from xml_lib import quick_validate
def test_xml_files_are_valid():
"""Ensure all XML files in project are valid."""
result = quick_validate(".")
assert result.is_valid, f"Found {len(result.errors)} validation errors"- Explore Pipeline Automation: See
examples/pipelines/for YAML-based pipeline examples - Interactive Shell: Try
xml-lib shellfor REPL-based exploration - Streaming for Large Files: Learn about streaming validation in
docs/STREAMING_GUIDE.md - Custom Guardrails: See
lib/guardrails/for rule definitions
- Documentation: Run
help(xml_lib)in Python - CLI Help: Run
xml-lib --help - Issues: https://github.com/farukalpay/xml-lib/issues