xml - VB 脚本修改 XML 文件节点

标签 xml xpath vbscript

这是一个示例 XML 文档内容:

<row>
    <RaceNumber>131</RaceNumber>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5149</Winner>
    <WinningVotes>6213</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner1>Jeff Kaufmann</Winner1>
    <WinnerSelected>1</WinnerSelected>
    <WinnerPercent>28</WinnerPercent>
    <Loser>5148</Loser>
    <LosingVotes>4628</LosingVotes>
    <LosingParty>R</LosingParty>
    <Loser2>Wayne Deerberg</Loser2>
    <LoserPercent>21</LoserPercent>
    <LoserSelected>1</LoserSelected>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5376</Winner>
    <WinningVotes>4407</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner>JonBell</Winner>
    <WinnerSelected>1</WinnerSelected>
    <PrecintPercent>100</PrecintPercent>
    <Loser>5151</Loser>
    <LosingVotes>4141</LosingVotes>
    <LosingParty>D</LosingParty>
    <Loser>DavidShinker</Loser>
    <LoserSelected>0</LoserSelected>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5150</Winner>
    <WinningVotes>3167</WinningVotes>
    <WinningParty>D</WinningParty>
    <Winner>RobertPruess</Winner>
    <WinnerSelected>0</WinnerSelected>
  </row>

我想知道是否可以使用 VB 脚本来修改该文件,使其看起来像这样?:

<row>
<ELECTION>    
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5149</Winner>
    <WinningVotes>6213</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner1>Jeff Kaufmann</Winner1>
    <WinnerSelected>1</WinnerSelected>
    <WinnerPercent>28</WinnerPercent>
    <Loser>5148</Loser>
    <LosingVotes>4628</LosingVotes>
    <LosingParty>R</LosingParty>
    <Loser2>Wayne Deerberg</Loser2>
    <LoserPercent>21</LoserPercent>
    <LoserSelected>1</LoserSelected>
</ELECTION>
<ELECTION>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5376</Winner>
    <WinningVotes>4407</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner>JonBell</Winner>
    <WinnerSelected>1</WinnerSelected>
    <PrecintPercent>100</PrecintPercent>
    <Loser>5151</Loser>
    <LosingVotes>4141</LosingVotes>
    <LosingParty>D</LosingParty>
    <Loser>DavidShinker</Loser>
    <LoserSelected>0</LoserSelected>
</ELECTION>

基本上,我想写<ELECTION>在任何标题为 <title1> 的标签之前和结束标签 </ELECTION>在任何标题为 </LoserSelected> 的标签之后

有人认为这是可能的吗?如果是这样,我想让它写<ELECTION></ELECTION>整个文档中的标签,其中 <title1></LoserSelected>会遇到的。

任何意见都会很棒!谢谢!

最佳答案

这(就像任何涉及 XML 的事情一样)是为 XML 设计的工具的工作。

XSLT 就是这样一个工具。抵制在 XML 上进行字符串替换的诱惑。

<!-- elections.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" encoding="UTF-8" />

  <xsl:key name="kElection" match="node()[not(self::title1)]" 
    use="generate-id(preceding-sibling::title1[1])"
  />

  <xsl:template match="row[ELECTION]">
    <xsl:copy-of select="." />
  </xsl:template>

  <xsl:template match="row[not(ELECTION)]">
    <xsl:copy>
      <xsl:apply-templates select="title1" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title1">
    <ELECTION>
      <xsl:copy-of select=". | key('kElection', generate-id(.))" />
    </ELECTION>
  </xsl:template>
</xsl:stylesheet>

您可以通过 VBScript 使用 MSXML API 来执行 XSLT 程序,如下所示:

Dim xml, xsl, xmlOut

Set xml = CreateObject("MSXML2.DOMDocument")
Set xsl = CreateObject("MSXML2.DOMDocument")

xml.async = False
xsl.async = False

xml.load "your_input.xml"
xsl.load "elections.xsl"

xml.transformNodeToObject xsl, xml
xml.save "your_output.xml"

但存在更简单的方法。你可以download the msxsl.exe tool并从命令行直接访问它:

msxsl your_input.xml elections.xsl -o your_output.xml
<小时/>

XSLT 程序的设计工作原理如下:

  • 它会单独保留已包含 <ELECTION> 的行元素(即,它只是将它们按原样复制到输出)。
  • 它使用 XSL 键按第一个 <title1> 对文件中的所有节点进行分组。在他们之前。这样可以一起检索属于某个标题的所有元素。
  • 看起来全部<title1>元素并制作它们及其关联组的副本,分别将它们包装在 <ELECTION> 中元素。

这种方法比简单的字符串替换更安全、更通用。

关于xml - VB 脚本修改 XML 文件节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979411/

相关文章:

windows - 无法等待进程

android - res>layout 中缺少文件

java - Android XzingScanner : How to customise ZxingScanner layout ?(添加按钮)

Java XML 解析问题

c# - 根据子节点对整个 xdocument 进行排序

ruby - 如何删除具有给定 src 属性的图像节点?

java - 如何将从 JSP 请求的 XML 数据格式化为表

java - 如何在Saxon xquery中查询一个查询结果

vbscript - 使用 msxml2.ServerXMLHTTP 将订阅 key 作为请求 header 传递 - 经典 ASP/VB

php - 从 javascript/php 在服务器上运行 VBScript