You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML节点替换及全节点复制操作需求(附示例XML)

How to Replace a Specific Node and Preserve All Existing Content in XML

Based on your sample XML, let’s say you want to update a specific node (for example, changing the <ID> value in the <Summary> section from 12345 to 67890) while keeping every other node, attribute, and piece of content intact. Here are two practical, reliable approaches to get this done:


Approach 1: Using XSLT (Extensible Stylesheet Language Transformations)

XSLT is purpose-built for XML transformations, and it’s perfect here because we can use the "identity template" to copy all content by default, then override only the node we want to modify.

Step 1: Create the XSLT Stylesheet

This stylesheet copies everything as-is except for the target <ID> node, which we replace with our new value:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Identity template: copies all nodes/attributes unchanged -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Override template for the specific ID node we want to replace -->
    <xsl:template match="Employee/Summary/ID">
        <ID>67890</ID>
    </xsl:template>
</xsl:stylesheet>

Step 2: Apply the Stylesheet

Use a tool like xsltproc (command-line) or a library in your preferred language to run the transformation. For example:

xsltproc transform.xsl input.xml > output.xml

The resulting XML will have all original content preserved, with only the <ID> value updated.


Approach 2: Using Python with lxml

If you prefer a scripting approach, Python’s lxml library makes this task simple and flexible.

Step 1: Install lxml (if not already installed)

pip install lxml

Step 2: Write the Script

This script loads your XML, locates the target node, updates its value, and saves the modified XML while retaining all other content:

from lxml import etree

# Load the input XML file
tree = etree.parse("input.xml")
root = tree.getroot()

# Find the target node using XPath (adjust this to match your specific node)
target_node = root.find(".//Employee/Summary/ID")

# Update the node's text content if it exists
if target_node is not None:
    target_node.text = "67890"

# Save the modified XML with proper formatting
tree.write(
    "output.xml",
    encoding="UTF-8",
    xml_declaration=True,
    pretty_print=True
)

Customization Tip

To replace a different node (like the <Pay> value or an <Effective_Date>), just adjust the XPath in the script or the match pattern in the XSLT. For example:

  • To target the <Pay> node under <Position>:
    • XSLT: <xsl:template match="Employee/Position/Pay">
    • Python: target_node = root.find(".//Employee/Position/Pay")

Both methods guarantee that all existing nodes, attributes, and content are preserved while only modifying the specific node you need to update.

内容的提问来源于stack exchange,提问作者CloudStrife103

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:34:42