replace only half of XML nodes



  • Hi,

    is there any way using regex to replace only half of a specific XML nodes?
    so for example, i have an XML with 100 nodes i would like to replace only 50 of them. it can be the first 50 occurrence or any 2nd occurrence.
    to be more specific to my use case.
    in the XML below each program element (there are 4 of them) has an external_id attribute.
    I would like to change the external_id value to something else but only for the 1st and 2nd program element or for the 1st and 3rd program element (every 2nd time)

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <EpgChannels parent-group-id="3199" group-id="3199" main-lang="eng" updater-id="0" xmlns="http://tempuri.org/xmltv">
    <channel id="4">
    <display-name lang="eng">FR2</display-name>
    </channel>
    <programme start="20201019000000" stop="20201019003000" channel="4" external_id="4_FR2_11414656841971">
    <title lang="eng">Paradigm</title>
    <desc lang="eng">Terms of the Geneva Convention come into play when the sister of a former inmate at Abu Ghraib prison is suspected of murdering a female Guardsman in the second Gulf War</desc>
    <icon ratio="boxcover_2_3" src="http://placeimg.com/1280/720/258659819.jpg"/>
    <metas>
    <MetaType>Release_Date</MetaType>
    <MetaValues lang="eng">2004-09-22</MetaValues>
    </metas>
    <metas>
    <MetaType>Ranking</MetaType>
    <MetaValues lang="eng">920</MetaValues>
    </metas>
    <metas>
    <MetaType>Audio_language</MetaType>
    <MetaValues lang="eng">en</MetaValues>
    </metas>
    <metas>
    <MetaType>Run_Time</MetaType>
    <MetaValues lang="eng">PT01H00M</MetaValues>
    </metas>
    <metas>
    <MetaType>SeriesID</MetaType>
    <MetaValues lang="eng">510997</MetaValues>
    </metas>
    <metas>
    <MetaType>SeasonNumber</MetaType>
    <MetaValues lang="eng">15</MetaValues>
    </metas>
    <metas>
    <MetaType>EpisodeNumber</MetaType>
    <MetaValues lang="eng">1</MetaValues>
    </metas>
    <metas>
    <MetaType>Country_of_Origin</MetaType>
    <MetaValues lang="eng">france</MetaValues>
    </metas>
    <metas>
    <MetaType>Summary_Short</MetaType>
    <MetaValues lang="eng">The sister of an inmate at Abu Ghraib is suspected of murdering a Guardsman.</MetaValues>
    </metas>
    <metas>
    <MetaType>CRID</MetaType>
    <MetaValues lang="eng">EP000176170348</MetaValues>
    </metas>
    <tags>
    <TagType>Genre</TagType>
    <TagValues lang="eng">Crime drama</TagValues>
    </tags>
    <tags>
    <TagType>SeriesName</TagType>
    <TagValues lang="eng">Law &amp; Order</TagValues>
    </tags>
    <tags>
    <TagType>Actors</TagType>
    <TagValues lang="eng">Joe Fontana</TagValues>
    <TagValues lang="eng">Ed Green</TagValues>
    <TagValues lang="eng">Lt. Anita Van Buren</TagValues>
    <TagValues lang="eng">Exec. Asst. D.A. Jack McCoy</TagValues>
    <TagValues lang="eng">Serena Southerlyn</TagValues>
    <TagValues lang="eng">Arthur Branch</TagValues>
    </tags>
    <tags>
    <TagType>ParentalRating</TagType>
    <TagValues lang="eng">CSA3</TagValues>
    </tags>
    <tags>
    <TagType>Provider</TagType>
    <TagValues lang="eng">FR2</TagValues>
    </tags>
    <tags>
    <TagType>Director</TagType>
    <TagValues lang="eng">Samantha Lueilwitz</TagValues>
    </tags>
    <enable-cdvr>true</enable-cdvr>
    <enable-catch-up>true</enable-catch-up>
    <enable-start-over>true</enable-start-over>
    <crid>EP000176170348</crid>
    </programme>
    <programme start="20201019003000" stop="20201019010000" channel="4" external_id="4_FR2_255393467237">
    <title lang="eng">The Dead Wives Club</title>
    <desc lang="eng">The ex-wife of a firefighter who married a rich Sept. 11 widow is suspected of murder.</desc>
    <icon ratio="boxcover_2_3" src="http://placeimg.com/1280/720/18880556.jpg"/>
    <metas>
    <MetaType>Release_Date</MetaType>
    <MetaValues lang="eng">2004-09-22</MetaValues>
    </metas>
    <metas>
    <MetaType>Ranking</MetaType>
    <MetaValues lang="eng">901</MetaValues>
    </metas>
    <metas>
    <MetaType>Audio_language</MetaType>
    <MetaValues lang="eng">en</MetaValues>
    </metas>
    <metas>
    <MetaType>Run_Time</MetaType>
    <MetaValues lang="eng">PT01H00M</MetaValues>
    </metas>
    <metas>
    <MetaType>SeriesID</MetaType>
    <MetaValues lang="eng">14694</MetaValues>
    </metas>
    <metas>
    <MetaType>SeasonNumber</MetaType>
    <MetaValues lang="eng">15</MetaValues>
    </metas>
    <metas>
    <MetaType>EpisodeNumber</MetaType>
    <MetaValues lang="eng">2</MetaValues>
    </metas>
    <metas>
    <MetaType>Country_of_Origin</MetaType>
    <MetaValues lang="eng">france</MetaValues>
    </metas>
    <metas>
    <MetaType>Summary_Short</MetaType>
    <MetaValues lang="eng">The ex-wife of a firefighter who married a rich Sept. 11 widow is suspected of murder.</MetaValues>
    </metas>
    <metas>
    <MetaType>CRID</MetaType>
    <MetaValues lang="eng">EP000176170349</MetaValues>
    </metas>
    <tags>
    <TagType>Genre</TagType>
    <TagValues lang="eng">Crime drama</TagValues>
    </tags>
    <tags>
    <TagType>SeriesName</TagType>
    <TagValues lang="eng">Law &amp; Order</TagValues>
    </tags>
    <tags>
    <TagType>Actors</TagType>
    <TagValues lang="eng">Joe Fontana</TagValues>
    <TagValues lang="eng">Ed Green</TagValues>
    <TagValues lang="eng">Lt. Anita Van Buren</TagValues>
    <TagValues lang="eng">Exec. Asst. D.A. Jack McCoy</TagValues>
    <TagValues lang="eng">Serena Southerlyn</TagValues>
    <TagValues lang="eng">Arthur Branch</TagValues>
    </tags>
    <tags>
    <TagType>ParentalRating</TagType>
    <TagValues lang="eng">CSA3</TagValues>
    </tags>
    <tags>
    <TagType>Provider</TagType>
    <TagValues lang="eng">FR2</TagValues>
    </tags>
    <tags>
    <TagType>Director</TagType>
    <TagValues lang="eng">Alejandro Fisher</TagValues>
    </tags>
    <enable-cdvr>true</enable-cdvr>
    <enable-catch-up>true</enable-catch-up>
    <enable-start-over>true</enable-start-over>
    <crid>EP000176170349</crid>
    </programme>
    <programme start="20201019010000" stop="20201019013000" channel="4" external_id="4_FR2_347177048622">
    <title lang="eng">Paradigm</title>
    <desc lang="eng">Terms of the Geneva Convention come into play when the sister of a former inmate at Abu Ghraib prison is suspected of murdering a female Guardsman in the second Gulf War</desc>
    <icon ratio="boxcover_2_3" src="http://placeimg.com/1280/720/525530802.jpg"/>
    <metas>
    <MetaType>Release_Date</MetaType>
    <MetaValues lang="eng">2004-09-22</MetaValues>
    </metas>
    <metas>
    <MetaType>Ranking</MetaType>
    <MetaValues lang="eng">133</MetaValues>
    </metas>
    <metas>
    <MetaType>Audio_language</MetaType>
    <MetaValues lang="eng">en</MetaValues>
    </metas>
    <metas>
    <MetaType>Run_Time</MetaType>
    <MetaValues lang="eng">PT01H00M</MetaValues>
    </metas>
    <metas>
    <MetaType>SeriesID</MetaType>
    <MetaValues lang="eng">225713</MetaValues>
    </metas>
    <metas>
    <MetaType>SeasonNumber</MetaType>
    <MetaValues lang="eng">15</MetaValues>
    </metas>
    <metas>
    <MetaType>EpisodeNumber</MetaType>
    <MetaValues lang="eng">1</MetaValues>
    </metas>
    <metas>
    <MetaType>Country_of_Origin</MetaType>
    <MetaValues lang="eng">france</MetaValues>
    </metas>
    <metas>
    <MetaType>Summary_Short</MetaType>
    <MetaValues lang="eng">The sister of an inmate at Abu Ghraib is suspected of murdering a Guardsman.</MetaValues>
    </metas>
    <metas>
    <MetaType>CRID</MetaType>
    <MetaValues lang="eng">EP000176170348</MetaValues>
    </metas>
    <tags>
    <TagType>Genre</TagType>
    <TagValues lang="eng">Crime drama</TagValues>
    </tags>
    <tags>
    <TagType>SeriesName</TagType>
    <TagValues lang="eng">Law &amp; Order</TagValues>
    </tags>
    <tags>
    <TagType>Actors</TagType>
    <TagValues lang="eng">Joe Fontana</TagValues>
    <TagValues lang="eng">Ed Green</TagValues>
    <TagValues lang="eng">Lt. Anita Van Buren</TagValues>
    <TagValues lang="eng">Exec. Asst. D.A. Jack McCoy</TagValues>
    <TagValues lang="eng">Serena Southerlyn</TagValues>
    <TagValues lang="eng">Arthur Branch</TagValues>
    </tags>
    <tags>
    <TagType>ParentalRating</TagType>
    <TagValues lang="eng">CSA3</TagValues>
    </tags>
    <tags>
    <TagType>Provider</TagType>
    <TagValues lang="eng">FR2</TagValues>
    </tags>
    <tags>
    <TagType>Director</TagType>
    <TagValues lang="eng">Ida Wilderman</TagValues>
    </tags>
    <enable-cdvr>true</enable-cdvr>
    <enable-catch-up>true</enable-catch-up>
    <enable-start-over>true</enable-start-over>
    <crid>EP000176170348</crid>
    </programme>
    <programme start="20201019013000" stop="20201019020000" channel="4" external_id="4_FR2_4358572412818">
    <title lang="eng">The Dead Wives Club</title>
    <desc lang="eng">The ex-wife of a firefighter who married a rich Sept. 11 widow is suspected of murder.</desc>
    <icon ratio="boxcover_2_3" src="http://placeimg.com/1280/720/807906304.jpg"/>
    <metas>
    <MetaType>Release_Date</MetaType>
    <MetaValues lang="eng">2004-09-22</MetaValues>
    </metas>
    <metas>
    <MetaType>Ranking</MetaType>
    <MetaValues lang="eng">279</MetaValues>
    </metas>
    <metas>
    <MetaType>Audio_language</MetaType>
    <MetaValues lang="eng">en</MetaValues>
    </metas>
    <metas>
    <MetaType>Run_Time</MetaType>
    <MetaValues lang="eng">PT01H00M</MetaValues>
    </metas>
    <metas>
    <MetaType>SeriesID</MetaType>
    <MetaValues lang="eng">188389</MetaValues>
    </metas>
    <metas>
    <MetaType>SeasonNumber</MetaType>
    <MetaValues lang="eng">15</MetaValues>
    </metas>
    <metas>
    <MetaType>EpisodeNumber</MetaType>
    <MetaValues lang="eng">2</MetaValues>
    </metas>
    <metas>
    <MetaType>Country_of_Origin</MetaType>
    <MetaValues lang="eng">france</MetaValues>
    </metas>
    <metas>
    <MetaType>Summary_Short</MetaType>
    <MetaValues lang="eng">The ex-wife of a firefighter who married a rich Sept. 11 widow is suspected of murder.</MetaValues>
    </metas>
    <metas>
    <MetaType>CRID</MetaType>
    <MetaValues lang="eng">EP000176170349</MetaValues>
    </metas>
    <tags>
    <TagType>Genre</TagType>
    <TagValues lang="eng">Crime drama</TagValues>
    </tags>
    <tags>
    <TagType>SeriesName</TagType>
    <TagValues lang="eng">Law &amp; Order</TagValues>
    </tags>
    <tags>
    <TagType>Actors</TagType>
    <TagValues lang="eng">Joe Fontana</TagValues>
    <TagValues lang="eng">Ed Green</TagValues>
    <TagValues lang="eng">Lt. Anita Van Buren</TagValues>
    <TagValues lang="eng">Exec. Asst. D.A. Jack McCoy</TagValues>
    <TagValues lang="eng">Serena Southerlyn</TagValues>
    <TagValues lang="eng">Arthur Branch</TagValues>
    </tags>
    <tags>
    <TagType>ParentalRating</TagType>
    <TagValues lang="eng">CSA3</TagValues>
    </tags>
    <tags>
    <TagType>Provider</TagType>
    <TagValues lang="eng">FR2</TagValues>
    </tags>
    <tags>
    <TagType>Director</TagType>
    <TagValues lang="eng">Laurie Stehr</TagValues>
    </tags>
    <enable-cdvr>true</enable-cdvr>
    <enable-catch-up>true</enable-catch-up>
    <enable-start-over>true</enable-start-over>
    <crid>EP000176170349</crid>
    </programme>
    </EpgChannels>
    
    

    thanks!
    ronen



  • Hello, @ronen-neumark and All,

    OK ! So, given this template :

    <programme start="20201019000000" stop="20201019003000" channel="4" external_id="4_FR2_11414656841971">
    ...
    ...
    ...
    </programme>
    <programme start="20201019003000" stop="20201019010000" channel="4" external_id="4_FR2_255393467237">
    ...
    ...
    ...
    </programme>
    <programme start="20201019010000" stop="20201019013000" channel="4" external_id="4_FR2_347177048622">
    ...
    ...
    ...
    </programme>
    <programme start="20201019013000" stop="20201019020000" channel="4" external_id="4_FR2_4358572412818">
    ...
    ...
    ...
    </programme>
    

    The following regex S/R would replace the external_id value with the litteral string 4_FR9_99999999999999, only one time out of two !

    SEARCH (?-s)(<programme.+external_id=").+(?s)(".+?</programme>\R<programme.+?</programme>)

    REPLACE ${1}4_FR9_99999999999999${2}

    And should give the expected result :

    <programme start="20201019000000" stop="20201019003000" channel="4" external_id="4_FR9_99999999999999">
    ...
    ...
    ...
    </programme>
    <programme start="20201019003000" stop="20201019010000" channel="4" external_id="4_FR2_255393467237">
    ...
    ...
    ...
    </programme>
    <programme start="20201019010000" stop="20201019013000" channel="4" external_id="4_FR9_99999999999999">
    ...
    ...
    ...
    </programme>
    <programme start="20201019013000" stop="20201019020000" channel="4" external_id="4_FR2_4358572412818">
    ...
    ...
    ...
    </programme>
    

    My regex also works with this simplified example !

    Best Regards,

    guy038


Log in to reply