首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用批处理脚本在XML文件中增加属性值

使用批处理脚本在XML文件中增加属性值
EN

Stack Overflow用户
提问于 2021-03-05 08:21:08
回答 1查看 126关注 0票数 0

我试图编辑一个xml文件并增加一个名为“pageindex”的属性值。

xml文件中的数据如下所示

代码语言:javascript
复制
?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"/> 

这是我创建的批处理脚本,但是值'1‘并没有增加。

代码语言:javascript
复制
    @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"

这是我运行球棒时得到的输出。您可以看到属性'pageindex‘不返回任何值。

代码语言:javascript
复制
?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"/>

我该怎么解决这个问题?这也是我第一次使用批处理脚本!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-05 09:18:45

第一个问题是搜索字符串_TAG=pageindex=

set "TEST=!LINE:*%_TAG%=!"中使用它时,结果是

代码语言:javascript
复制
==1,pagingsize=350"

等号加倍,因为serach表达式不能包含等号,所以等号总是用于拆分搜索和替换部分。

您搜索pageindex并将其替换为=

因此,%%S包含==1,而set /a NUM===1+1失败

您可以通过将delims=,更改为

代码语言:javascript
复制
for /F "tokens=1* eol=, delims=,= " %%S in ("!TEST!") do (
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66489056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档