0

I am trying to edit an xml file and increment an attribute value called 'pageindex'.

The data in the xml file looks like this

?xml version="1.0" encoding="UTF-8"?>
<quer:query productCode="RC1700" model="http://www.taleo.com/ws/tee800/2009/01" projectedClass="Candidate" locale="en" alias="MainQuery" mode="T-XML" largegraph="true"
preventDuplicates="false" attributes="pageindex=1,pagingsize=350" 
xmlns:quer="http://www.taleo.com/ws/integration/query">
<quer:subQueries/><quer:projections>
<quer:projection><quer:field path="Number"/>
</quer:projection><quer:projection><quer:field path="FirstName"/> 

This is the batch script I created but the value '1' is not getting incremented.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "_FILE=pagexml" & rem // (input file; `%~1` is the first argument)
    set "_INI=<quer:query productCode="RC1700" model="http://www.taleo.com/ws/tee800/2009/01" projectedClass="Candidate" locale="en" alias="MainQuery" mode="T-XML" largegraph="true" preventDuplicates="false" attributes=" &
    set "_TAG=pageindex=" & rem // (opening tag)
    
    rem // Loop over all (non-empty) lines of the input file:
    (for /F "usebackq delims=" %%L in ("%_FILE%") do (
        rem // Store current line string:
        set "LINE=%%L"
        rem // Toggle delayed expansion to avoid troubles with `!`:
        setlocal EnableDelayedExpansion
        rem // Split off opening tag from line string:
        set "TEST=!LINE:*%_TAG%=!"
        rem // Check whether opening tag has been found:
        if not "!TEST!"=="!LINE!" (
            rem // Opening tag found, hence split off closing tag:
            for /F "tokens=1* eol=, delims=, " %%S in ("!TEST!") do (
                rem // Get extracted number and increment it:
                set /A "NUM=%%S+1"
            rem // Return rebuild line with incremented number:
                echo( !_INI!!_TAG!!NUM!^,%%T
            )
        ) else (
            rem // Opening tag not found, hence return original line:
            echo(!LINE!
        )
        endlocal
    ))>pageTmp.xml 
    
    
    copy /v /y "pageTmp.xml" "page.xml"

del "pageTmp.xml"

This is the output I get when I run the bat. You can see that the attribute 'pageindex' does not return any value.

?xml version="1.0" encoding="UTF-8"?>
<quer:query productCode="RC1700" model="http://www.taleo.com/ws/tee800/2009/01" projectedClass="Candidate" locale="en" alias="MainQuery" mode="T-XML" largegraph="true"
preventDuplicates="false" attributes="pageindex=,pagingsize=350" 
xmlns:quer="http://www.taleo.com/ws/integration/query">
<quer:subQueries/><quer:projections>
<quer:projection><quer:field path="Number"/>
</quer:projection><quer:projection><quer:field path="FirstName"/>

How do I fix this? Also this is my first time working with a batch script!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Coding
  • 11
  • 3
  • 2
    I have seen this code [somewhere else](https://stackoverflow.com/a/62074885)… ;-) Anyway, to edit XML data you should use a language that natively supports it (like PowerShell, VBScript, JavaScript, for example)… – aschipfl Mar 05 '21 at 10:06
  • It does not appear that the given text is considered a valid XML file. Try with PowerShell using `[xml](Get-Content -Path 'C:\src\t\quer.xml')`. – lit Mar 05 '21 at 15:41

1 Answers1

1

The first problem is your search string _TAG=pageindex=.
When using it in set "TEST=!LINE:*%_TAG%=!", this results to

==1,pagingsize=350"

The equal sign is doubled, because the serach expression can't contain an equal sign, the equal sign is always used to split the search and replace part.
You search for pageindex and replace it with =

Therefore %%S contains ==1 and set /a NUM===1+1 fails

You can solve it by changing delims=, to delims=,= in

for /F "tokens=1* eol=, delims=,= " %%S in ("!TEST!") do (
jeb
  • 78,592
  • 17
  • 171
  • 225