Search code examples
xmlreplaceattributesfind

In Notepad++, Is there a way to Find/Replace every instance of an XML attribute even when it has different values?


I have an attribute under the catalogSeqNumber tag called catalogSeqNumberValue (CSNV), and you can give it a number as its value, like this::

<catalogSeqNumberValue="1">

In this document, there are 20 instances of this attribute, numbered 1 through 20.

What I want to do is do a Find/Replace that will highlight all of them (including the value) so I can replace them all at once with the same value.

If it works, I would be able to replace all 20 instances of this tag with just one, so they will all have the same value.

I can't just do a Find/Replace for "1" because that matches other attributes that aren't related. I know I could just go in an replace them manually, but there are hundreds of them, and I'd like to be able to put all the XML files in a folder and use Notepad++ to replace them all in the folder.

Is this possible?


Solution

  • Here is a conceptual example via XSLT.

    Input XML

    <?xml version="1.0"?>
    <root>
        <catalogSeqNumber catalogSeqNumberValue="1"/>
        <catalogSeqNumber catalogSeqNumberValue="10"/>
        <catalogSeqNumber catalogSeqNumberValue="18"/>
    </root>
    

    XSLT

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
        <xsl:strip-space elements="*"/>
    
        <!--Identity Transform pattern-->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="@catalogSeqNumberValue">
            <xsl:attribute name="catalogSeqNumberValue">
                <xsl:text>770</xsl:text>
            </xsl:attribute>
        </xsl:template>
    </xsl:stylesheet>
    

    Output

    <?xml version='1.0' encoding='utf-8' ?>
    <root>
      <catalogSeqNumber catalogSeqNumberValue="770"/>
      <catalogSeqNumber catalogSeqNumberValue="770"/>
      <catalogSeqNumber catalogSeqNumberValue="770"/>
    </root>