1

我创建了一个用于无人值守 Windows7 安装的应答文件。我希望能够即时修改一些设置(时区、计算机名称等),但我是 VBScript/XML 的新手。我在这个站点上找到了一篇整洁的文章VBScript Find a node in XML node and replace the value on how to use xpath。我的一些麻烦是针对节点(我认为),因为我还没有找到使用格式的示例。我试过使用 full 和 just ,但在完整的答案文件中有几个具有相同组件名称的节点。建议...请?:)

<unattend xmlns="urn.schemas-microsoft.com:unattend">        
    <settings pass="specialize">
        <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="*REMOVED*" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ProductKey>*REMOVED*</ProductKey>
            <RegisteredOwner>*REMOVED*</RegisteredOwner>
            <DisableAutoDaylightTimeSet>false</DisableAutoDaylightTimeSet>
            <ComputerName>*</ComputerName>
            <DoNotCleanTaskBar>true</DoNotCleanTaskBar>
            <BluetoothTaskbarIconEnabled>false</BluetoothTaskbarIconEnabled>
            <CopyProfile>true</CopyProfile>
            <ShowWindowsLive>false</ShowWindowsLive>
            <TimeZone>VarTime</TimeZone>
        </component>
    </settings>
</unattend>

与 VB 混在一起,我能够自己想出一个人。我很欣赏这篇文章。这也会提示输入用户框。有什么理由为什么这样的事情不能有效地完成工作吗?

Set xml = CreateObject("Msxml2.DOMDocument.3.0")

xml.Async = "False"
xml.load "path.xml"

strTime = InputBox("Please Select your Time Zone.")
strTimeZone = "Nothing"         

if strTime= "1" then strTimeZone= "Eastern Standard Time"
if strTime= "2" then strTimeZone= "Central Standard Time"
if strTime= "3" then strTimeZone= "Mountian Standard Time"
if strTime= "4" then strTimeZone= "Pacific Stardard Time"

Set TimeZone = xml.selectSingleNode("//unattend/settings/component/TimeZone")


TimeZone.Text = strTimeZone

'Save the xml document with the new settings.
strResult = xml.save("path.xml")
4

1 回答 1

0

这个脚本

  Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\testdata\xml\ns-xpath-01.xml")
  Dim sNS    : sNS      = "xmlns:a='urn.schemas-microsoft.com:unattend'"
  Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument")
  oXML.setProperty "SelectionLanguage", "XPath"
  oXML.setProperty "SelectionNamespaces", sNS
  oXML.async = False
  oXML.load sFSpec
  If 0 = oXML.parseError Then
     WScript.Echo oXML.xml
     WScript.Echo "-----------------"
     Dim sXPath : sXPath    = "/a:unattend/a:settings/a:component/a:TimeZone"
     Dim ndFnd  : Set ndFnd = oXML.selectSingleNode(sXPath)
     If ndFnd Is Nothing Then
        WScript.Echo sXPath, "not found"
     Else
        WScript.Echo ndFnd.text
        WScript.Echo "-----------------"
        ndFnd.text = "Abracadabra"
        WScript.Echo oXML.xml
     End If
  Else
     WScript.Echo oXML.parseError.reason
  End If

输出:

<unattend xmlns="urn.schemas-microsoft.com:unattend">
        <settings pass="specialize">
                <component>
                        <TimeZone>VarTime</TimeZone>
                </component>
        </settings>
</unattend>

-----------------
VarTime
-----------------
<unattend xmlns="urn.schemas-microsoft.com:unattend">
        <settings pass="specialize">
                <component>
                        <TimeZone>Abracadabra</TimeZone>
                </component>
        </settings>
</unattend>

展示了如何在 XPath 表达式中使用SelectionNamespaces属性和前缀。

PS在这里查看如何查找/更改属性(使用基本相同的代码) 。

PP:

更多相同。

于 2012-07-30T19:48:48.063 回答