XSLT Edit XML

Modify and transform XML data with XSLT

✏️ What is XSLT Editing?

XSLT can transform and modify XML documents by adding, removing, or changing elements. You can restructure data, filter content, and create new XML formats from existing ones.


<!-- Original XML -->
<person>
    <name>John</name>
</person>
                                    

XSLT Editing Operations

XSLT provides powerful tools to manipulate XML data. You can add new elements, modify existing content, remove unwanted data, and reorganize structure to create transformed output that meets your specific needs.

Add Elements

Insert new XML elements

✏️

Modify Content

Change element values

🗑️

Remove Elements

Filter out unwanted data

🔄

Reorganize

Restructure XML layout

🔹 Adding New Elements

Use XSLT to add new elements to your XML:

📄 Input XML:

<?xml version="1.0"?>
<person>
    <name>Alice</name>
</person>

📄 XSLT Stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="person">
        <person>
            <xsl:copy-of select="name"/>
            <!-- Add new element -->
            <status>Active</status>
        </person>
    </xsl:template>
</xsl:stylesheet>

📄 Output XML:

<person>
    <name>Alice</name>
    <status>Active</status>
</person>

🔹 Modifying Element Values

Change the content of existing elements:

📄 Input XML:

<product>
    <price>100</price>
</product>

📄 XSLT Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="price">
        <price>
            <!-- Add 10% to price -->
            <xsl:value-of select=". * 1.10"/>
        </price>
    </xsl:template>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

📄 Output XML:

<product>
    <price>110</price>
</product>

🔹 Removing Elements

Filter out unwanted elements from XML:

📄 Input XML:

<users>
    <user status="active">John</user>
    <user status="inactive">Jane</user>
    <user status="active">Bob</user>
</users>

📄 XSLT Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="users">
        <users>
            <!-- Only copy active users -->
            <xsl:copy-of select="user[@status='active']"/>
        </users>
    </xsl:template>
</xsl:stylesheet>

📄 Output XML:

<users>
    <user status="active">John</user>
    <user status="active">Bob</user>
</users>

🔹 Reorganizing Structure

Change the structure and order of XML elements:

📄 Input XML:

<book>
    <title>XML Guide</title>
    <author>John Smith</author>
    <year>2024</year>
</book>

📄 XSLT Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="book">
        <publication>
            <info>
                <xsl:value-of select="title"/>
                <xsl:text> by </xsl:text>
                <xsl:value-of select="author"/>
            </info>
            <published><xsl:value-of select="year"/></published>
        </publication>
    </xsl:template>
</xsl:stylesheet>

📄 Output XML:

<publication>
    <info>XML Guide by John Smith</info>
    <published>2024</published>
</publication>

🔹 Renaming Elements

Change element names while keeping content:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Rename 'firstname' to 'first-name' -->
    <xsl:template match="firstname">
        <first-name>
            <xsl:value-of select="."/>
        </first-name>
    </xsl:template>
    
    <!-- Copy everything else -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

🔹 Common Editing Patterns

Useful XSLT Elements:

  • <xsl:copy-of> - Copy elements as-is
  • <xsl:value-of> - Extract element values
  • <xsl:element> - Create new elements dynamically
  • <xsl:attribute> - Add attributes to elements
  • <xsl:if> - Conditional element creation
  • <xsl:choose> - Multiple condition handling

🧠 Test Your Knowledge

Which XSLT element is used to copy existing elements?