0

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!

5
  • 1
    Notepad++ has XML Tools plugin. That plugin supports XSLT. XSLT will help you to achieve what you need. Commented Jan 18, 2024 at 21:17
  • 1
    Please edit your question and add the following: (1) Well-formed input XML. (2) Your logic of what needs to be done. (3) Desired output, based on the sample XML in the #1 above. Commented Jan 18, 2024 at 21:19
  • All within the question as text, no images. Commented Jan 18, 2024 at 21:33
  • I tried to include as text but it would remove the content within chevrons. is there a way to avoid that? Commented Jan 18, 2024 at 21:39
  • You need to select it (highlight it) and click Ctrl+K Commented Jan 18, 2024 at 21:40

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

apologies, but how do i implement this? i am new to working with these tools. I installed the xml plugin though
I added it to the answer.
so it seemed to make the edit, but it created a new text doc in doing so instead of updating the xml file itself
That's correct and expected behavior. It allows the following: (1) preserve the original document, and (2) compare with the newly created XML file. If you don't need it, you can delete the original XML file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.