Chapter 69: XSLT Editing XML

1. The honest reality in 2025–2026: “Editing XML” with XSLT

When people say “edit XML with XSLT”, they almost never mean:

  • Open file → change one line → save

They mean:

Take an existing XML document Make small or large modifications to it (add/remove/change elements/attributes/text) Produce a new (modified) XML document as output

This is called the identity transform + modifications pattern — the single most common real-world use case of XSLT in 2025–2026.

Why? Because:

  • Many enterprise/government/finance/healthcare systems require XML as input and output
  • You often need to add missing fields, remove sensitive data, rename tags, add digital signatures, convert namespaces, filter content, enrich with extra metadata
  • Doing this with string manipulation (regex, DOM manipulation in Java/Python) is fragile and error-prone
  • XSLT was designed exactly for this — safe, declarative, schema-aware transformation

2. The most important pattern: Identity Transform + Modifications

Identity transform = copy everything as-is, unless told otherwise.

This is the foundation of almost every “edit XML” stylesheet.

Classic identity template (XSLT 1.0 style)

XML
  • @* = all attributes
  • node() = all nodes (elements, text, comments, processing instructions…)
  • <xsl:copy> = copy current node (tag name, attributes, namespace)
  • <xsl:apply-templates> = continue processing children

Result: the output is identical to the input — unless you add more specific templates that override parts.

3. Full realistic example – Common “edit XML” tasks

Input XML (before modification – invoice.xml)

XML

Common editing tasks we will do (one by one):

  1. Add a new element <cbc:Note> with a message
  2. Change <cbc:IssueDate> to current date
  3. Add an attribute issueTime to <cbc:IssueDate>
  4. Remove <cac:TaxTotal> if tax amount = 0
  5. Rename <cbc:PayableAmount> to <cbc:GrandTotal>
  6. Add a digital signature placeholder element

XSLT stylesheet (edit-invoice.xsl)

XML

How to test this

Using command line (if you have Saxon or xsltproc)

Bash

Or using online tools (for quick testing):

  • https://www.freeformatter.com/xslt-transformer.html
  • https://www.utilities-online.info/xslt/
  • Oxygen XML Editor (free trial) or VS Code with XSLT extension

Summary – Most common “edit XML” tasks with XSLT

Task Typical pattern / technique Key instruction / tip
Add new element at specific position Match preceding element → output copy + new element Use xsl:copy-of + insert after
Change element text <xsl:template match=”title”> → <xsl:value-of select=”new-value”/> Use textContent in JS, xsl:value-of in XSLT
Change / add attribute xsl:copy + xsl:attribute or setAttribute equivalent <xsl:attribute name=”new”>value</xsl:attribute>
Remove element Match it → output nothing Most elegant way in XSLT
Rename element Match old name → output new tag name + copy content <xsl:element name=”NewName”> + `<xsl:copy-of select=”@*
Move node to different position Copy it → remove original → paste copy Two templates: one to copy, one to skip original
Conditional addition/removal Use <xsl:if> or <xsl:choose> around copy Very common for enrichment / filtering

Would you like to continue with one of these next?

  • Identity transform + small modifications (copy most, change few)
  • Removing nodes conditionally (out-of-stock, sensitive data)
  • Moving nodes (reordering elements)
  • Adding namespaces / signatures (real GST/UBL patterns)
  • Replacing whole subtrees (update entire sections)
  • Debugging when changes don’t appear or XML breaks

Just tell me which direction you want to go next! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *