这是一个示例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>
。
任何输入都会很棒!谢谢!
答案 0 :(得分:1)
这(与涉及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>
个元素的行(即它只是将它们复制到输出中)。<title1>
对文件中的所有节点进行分组。这样,可以一起检索属于某个标题的所有元素。<title1>
元素并制作它们及其关联组的副本,分别将它们包装在<ELECTION>
个元素中。这种方法比做天真的弦乐替换更安全,更通用。
答案 1 :(得分:-1)
这应该做你要问的事。它会在<ELECTION>
之后<title1>
和</ELECTION>
之后</LoserSelected>
粘贴Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadXmlFile = objFSO.OpenTextFile("file.xml")
Set objNewXmlFile = objFSO.CreateTextFile("file.new.xml", True)
boolSkipCloseElection = False
strPrevLine = ""
Do While Not objReadXmlFile.AtEndOfStream
strLine = objReadXmlFile.ReadLine()
If InStr(1, strLine, "<title1>") Then
If InStr(1, strPrevLine, "<ELECTION>") = 0 Then
objNewXmlFile.Write "<ELECTION>" & vbCrLf
Else
boolSkipCloseElection = True
End If
objNewXmlFile.Write strLine & vbCrLf
ElseIf InStr(1, strLine, "</LoserSelected>") Then
objNewXmlFile.Write strLine & vbCrLf
If boolSkipCloseElection = True Then
boolSkipCloseElection = False
Else
objNewXmlFile.Write "</ELECTION>" & vbCrLf
End If
Else
objNewXmlFile.Write strLine & vbCrLf
End If
strPrevLine = strLine
Loop
objReadXmlFile.Close
objNewXmlFile.Close
{{1}}
编辑:如果已存在,则不会插入这些标记。