gpt4 book ai didi

xml - VBScript从XML节点分配变量

转载 作者:行者123 更新时间:2023-12-03 16:33:10 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的VBScript,该文件将查找XML文件,提取两个属性,将它们添加,并将结果添加到输出文件中。到目前为止,我已经能够使用以下命令加载XML输入文件:

Option Explicit
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\_STATS_SCRIPTS\STATS_HOME.xml"


然后,我创建并初始化了这样的变量:

Dim fumlost
Dim intercept
Dim turnovers
fumlost="0"
intercept="0"
turnovers="0"


这是我的XML的样子(在合计子树之后被截断):

<fbgame source="Tas Football" version="4.16.01" generated="9/3/2014">
<venue gameid="01UA-WVU" visid="WVU" homeid="UA" visname="West Virginia" homename="Alabama" date="8/30/2014" location="Atlanta, Georgia" stadium="Georgia Dome" start="3:36" end="7:05" neutralgame="Y" duration="3:29" attend="70502" temp="" wind="" weather="Indoor">
<officials ref="David Epperly" ump="Mike Webster" line="Steve Clein" lj="Rod Pearson" bj="Pat Ryan" fj="Mike Culler" sj="Eddie Bonet"></officials>
<notes>
<note text="Replay: Dan Post"></note>
</notes>
<rules qtrs="4" mins="15" downs="4" yds="10" kospot="35" tbspot="20" kotbspot="25" patspot="3" safspot="20" td="6" fg="3" pat="1" patx="2" saf="2" defpat="2" rouge="1" field="100" toh="3" sackrush="Y" fgaplay="Y" netpunttb="Y"></rules>
</venue>
<team vh="H" code="8" id="UA" name="Alabama" record="1-0" abb="A">
<linescore prds="4" line="3,17,10,3" score="33">
<lineprd prd="1" score="3"></lineprd>
<lineprd prd="2" score="17"></lineprd>
<lineprd prd="3" score="10"></lineprd>
<lineprd prd="4" score="3"></lineprd>
</linescore>
<totals totoff_plays="82" totoff_yards="538" totoff_avg="6.6">
<firstdowns no="30" rush="13" pass="14" penalty="3"></firstdowns>
<penalties no="7" yds="49"></penalties>
<conversions thirdconv="9" thirdatt="15" fourthconv="0" fourthatt="1"></conversions>
<fumbles no="0" lost="0"></fumbles>
<misc yds="0" top="37:47" ona="0" onm="0" ptsto="0"></misc>
<redzone att="4" scores="4" points="24" tdrush="3" tdpass="0" fgmade="1" endfga="0" enddowns="0" endint="0" endfumb="0" endhalf="0" endgame="0"></redzone>
<rush att="49" yds="288" gain="294" loss="6" td="3" long="26"></rush>
<pass comp="24" att="33" int="1" yds="250" td="0" long="38" sacks="0" sackyds="0"></pass>
<rcv no="24" yds="250" td="0" long="38"></rcv>
<punt no="2" yds="101" long="62" blkd="0" tb="0" fc="1" plus50="1" inside20="1" avg="50.5"></punt>
<ko no="7" yds="453" ob="0" tb="3"></ko>
<fg made="4" att="4" long="47" blkd="0"></fg>
<pat kickatt="3" kickmade="3"></pat>
<defense tackua="34" tacka="38" tot_tack="72" tflua="6" tfla="0" tflyds="30" sacks="3" sackyds="25" brup="3"></defense>
<kr no="4" yds="99" td="0" long="26"></kr>
<pr no="1" yds="-1" td="0" long="0"></pr>
<scoring td="3" fg="4" patkick="3"></scoring>
</totals>


接下来我需要做的是将/ fbgame / team / totals / fumbles @ lost分配给我的fumbles变量,将/ fbgame / team / totals / pass @ int分配给我的拦截变量,然后将两者加在一起以产生失误,然后输出。我想我可以处理变量的求和并输出文件,但是我对如何获取分配给变量的XML属性一无所知。早些时候,我成功地制作了一个脚本,该脚本使用sXPath将访问团队和主队从我的主要输入文件中分离出来,但是我目前无法使用在此学到的知识来完成此任务!

我非常感激任何帮助,因为我是n00b脚本编写者,有点不知所措!

最佳答案

不确定在VBScript中使用XPath,但是以下XPath对我有用:

string(//fbgame/team/totals/fumbles/@lost)


结果:0

string(//fbgame/team/totals/pass/@int)


结果:1

也许这适合您的方法,或者您可以进一步调整它。

如果您需要整个节点,而不仅需要值,请遵循XPath

//fbgame/team/totals/fumbles[@lost]


结果是

 <fumbles no="0" lost="0" />


为了提供完整性-根据查询, //fbgame可以是 /fbgame。我只是将XML部分调整为对解析有效,然后让XPath匹配每个 fbgame(因为该示例仅包含一个游戏)。

如果问题不是关于XPath表达式而是关于如何在VBScript中获取XPath值,则应该这样做(至少要获取该值,猜想您会将这些值存储在变量中以便以后进行数学运算):

For Each a In objDoc.selectNodes ("//fbgame/team/totals/fumbles/@lost")
Wscript.Echo a.text
Next

For Each b In objDoc.selectNodes ("//fbgame/team/totals/pass/@int")
Wscript.Echo b.text
Next

关于xml - VBScript从XML节点分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com