XSLT – Editing XML

Welcome to The Coding College! In this article, we’ll dive into how XSLT can be used to edit XML data by transforming it into a modified XML document. While XSLT cannot directly “edit” an XML file (as it works on transformations rather than in-place modifications), it is an excellent tool for generating a new version of XML with the required changes.

What Does It Mean to Edit XML with XSLT?

When we “edit” XML with XSLT, we’re transforming the original XML document into a modified version by:

  1. Adding or removing elements or attributes.
  2. Renaming elements or attributes.
  3. Reorganizing the structure of the XML.
  4. Updating the content of elements or attributes.

XSLT makes these edits during the transformation process, creating a new XML document as the output.

Why Use XSLT for Editing XML?

  • Declarative Approach: You define the desired output structure using XSLT templates, making it easy to manage transformations.
  • Automation: Once defined, XSLT can repeatedly apply the same transformation logic to different XML files.
  • Cross-Platform Compatibility: XSLT is supported across various platforms and programming languages.

Key XSLT Techniques for Editing XML

1. Adding New Elements or Attributes

XSLT allows you to insert new elements or attributes into the transformed XML.

2. Removing Unwanted Data

You can filter out elements or attributes that are no longer needed.

3. Renaming Elements or Attributes

Use XSLT to rename elements or attributes for compatibility with new schemas.

4. Updating Values

Modify the values of elements or attributes during the transformation.

Example: Editing XML with XSLT

Let’s go through an example to demonstrate how XSLT can be used to edit an XML file.

Input XML: books.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book id="1">
    <title>Learning XML</title>
    <author>John Doe</author>
    <price currency="USD">29.99</price>
  </book>
  <book id="2">
    <title>Advanced XSLT</title>
    <author>Jane Smith</author>
    <price currency="USD">39.99</price>
  </book>
</library>

XSLT Stylesheet: edit-books.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- Root template -->
  <xsl:template match="/">
    <updatedLibrary>
      <!-- Apply templates to all book elements -->
      <xsl:apply-templates select="library/book" />
    </updatedLibrary>
  </xsl:template>

  <!-- Template for book elements -->
  <xsl:template match="book">
    <book>
      <!-- Rename the id attribute to bookID -->
      <xsl:attribute name="bookID">
        <xsl:value-of select="@id" />
      </xsl:attribute>

      <!-- Add a new 'category' element -->
      <category>Programming</category>

      <!-- Copy the title element -->
      <title>
        <xsl:value-of select="title" />
      </title>

      <!-- Copy the author element -->
      <author>
        <xsl:value-of select="author" />
      </author>

      <!-- Modify the price element -->
      <price>
        <xsl:value-of select="price * 0.9" /> <!-- Apply a 10% discount -->
      </price>
    </book>
  </xsl:template>

</xsl:stylesheet>

Explanation of the XSLT

  1. Renaming Attributes: The id attribute is renamed to bookID.
  2. Adding a New Element: A <category> element is added with the value Programming.
  3. Copying Elements: Existing elements like <title> and <author> are copied without changes.
  4. Updating Values: The <price> element is updated to reflect a 10% discount.

Output XML: Edited Version

<?xml version="1.0" encoding="UTF-8"?>
<updatedLibrary>
  <book bookID="1">
    <category>Programming</category>
    <title>Learning XML</title>
    <author>John Doe</author>
    <price>26.991</price>
  </book>
  <book bookID="2">
    <category>Programming</category>
    <title>Advanced XSLT</title>
    <author>Jane Smith</author>
    <price>35.991</price>
  </book>
</updatedLibrary>

Real-World Applications

1. XML Data Migration

When moving XML data from one schema to another, XSLT can edit and transform the data into the required structure.

2. XML Cleanup

XSLT can remove unwanted or redundant elements and attributes from XML files.

3. Content Updates

Make bulk updates to XML content, such as applying discounts, adding new elements, or standardizing values.

4. Data Integration

Transform XML data into a consistent format to integrate it with other systems or applications.

Tools for Server-Side XSLT Processing

  • SAXON: An advanced XSLT processor supporting XSLT 2.0 and 3.0.
  • XSLTProcessor (PHP): PHP’s built-in processor for applying XSLT transformations.
  • lxml (Python): A Python library that supports XSLT for XML editing.
  • System.Xml.Xsl (C#): A .NET library for server-side XSLT transformations.

Conclusion

XSLT is a powerful tool for transforming and editing XML documents. By using XSLT templates, you can easily rename elements, update values, and restructure XML data. Whether you’re preparing XML for integration, applying business logic, or migrating to a new schema, XSLT offers a declarative and efficient approach.

For more tutorials on XML, XSLT, and related technologies, visit The Coding College and take your XML editing skills to the next level!

Leave a Comment