Bulk replace values in XML file through Notepad++

71 Views Asked by At

I am trying to do a bulk update where I copy the values of one tag to another but have not had any luck. Would appreciate any help that could be provided!

Here is an example of what want to do.

Old
<name>ABC</name>
<description>old text</description>

New
<name>ABC</name>
<description>ABC</description>

Thanks!

1

There are 1 best solutions below

4
Yitzhak Khabinsky On BEST ANSWER

Please try the following solution based on XSLT.

It is using a so called Identity Transform pattern.

Notable points:

  • The XSLT will copy the entire input XML as-is except the <description> elements.
  • Each <description> elements will get value of the sibling <name> element.

In the Notepad++ select the following option in the menu:

Plugins => XML Tools => XSL Transformation...

Input XML

<?xml version="1.0"?>
<root>
    <item id="1">
        <name>ABC</name>
        <description>old text</description>
    </item>
    <item id="2">
        <name>BBC</name>
        <description>some text</description>
    </item>
</root>

XSLT 1.0

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"
                encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!--Identity transform-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="description">
        <xsl:copy>
            <xsl:value-of select="preceding-sibling::name"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output XML

<root>
  <item id="1">
    <name>ABC</name>
    <description>ABC</description>
  </item>
  <item id="2">
    <name>BBC</name>
    <description>BBC</description>
  </item>
</root>