使用带有 vbscript 的 xpath 编辑 xml 文件时遇到问题

Trouble using xpath with vbscript to edit xml file

我正在尝试使用 vbs 编辑一个相对较大的 xml 文件中的一个属性。每当我尝试使用 selectSingleNode 操作时,我都会收到错误消息。这是一个缩短的 xml 文件,应该提供所有需要的信息。我需要将根记录器节点的 编辑为 WARN 而不是 INFO

<?xml version="1.0" encoding="UTF-8"?>
<server xmlns="urn:jboss:domain:1.2">
<extensions>
    <extension module="org.jboss.as.clustering.infinispan"/>
    <extension module="org.jboss.as.cmp"/>
    <extension module="org.jboss.as.configadmin"/>
    <extension module="org.jboss.as.connector"/>
    <extension module="org.jboss.as.deployment-scanner"/>
    <extension module="org.jboss.as.ee"/>
    <extension module="org.jboss.as.ejb3"/>
    <extension module="org.jboss.as.jacorb"/>
        <extension module="org.jboss.as.jaxr"/>
        <extension module="org.jboss.as.jaxrs"/>
        <extension module="org.jboss.as.jdr"/>
        <extension module="org.jboss.as.jmx"/>
        <extension module="org.jboss.as.jpa"/>
        <extension module="org.jboss.as.jsr77"/>
        <extension module="org.jboss.as.logging"/>
    </extensions>
    <management>
        <security-realms>
            <security-realm name="ManagementRealm">
                <authentication>
                    <properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/>
                </authentication>
            </security-realm>
            <security-realm name="ApplicationRealm">
                <authentication>
                    <properties path="application-users.properties" relative-to="jboss.server.config.dir"/>
                </authentication>
            </security-realm>
        </security-realms>
        <management-interfaces>
            <native-interface security-realm="ManagementRealm">
                <socket-binding native="management-native"/>
            </native-interface>
            <http-interface security-realm="ManagementRealm">
                <socket-binding http="management-http"/>
            </http-interface>
        </management-interfaces>
    </management>
    <profile>
        <subsystem xmlns="urn:jboss:domain:logging:1.1">
            <logger category="com.arjuna">
                <level name="WARN"/>
            </logger>
            <logger category="org.apache.tomcat.util.modeler">
                <level name="WARN"/>
            </logger>
            <root-logger>
                <level name="INFO"/>
                <handlers>
                    <handler name="CONSOLE"/>
                    <handler name="FILE"/>
                </handlers>
            </root-logger>
        </subsystem>
    </profile>
</server> 

我试图用来编辑文件的脚本如下所示

set xml = CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage", "XPath"
xml.setProperty "SelectionNamespaces", "xmlns:m='urn:jboss:domain:logging:1.1'"
slog4j = "WARN"

err.clear
on error resume next
xml.load (WScript.arguments(0))
if (err.number = 0) then
xml.selectSingleNode("//profile/subsystem/root-logger/level/@").text = slog4j
strResult = xml.save(WScript.arguments(0))
end if

它似乎在正确读取 xml 文件并且在获取属性的 xpath 位置上有问题,但我使用了与以前文件相同的格式。任何建议都会很棒。对于 xml 文件的长代码块,我们深表歉意。我不知道我可以删除多少,同时仍然提供足够的信息来获得一些帮助

我是 XPath 的新手,但您应该能够 select 元素本身,然后使用 attributes 属性 更新属性值:

xml.selectSingleNode("//profile/subsystem/root-logger/level").attributes.item(0).text = slog4j

编辑:

好的,显然这只适用于使用 XSL 模式作为 selection 语言而不是 XPath,因此您必须注释掉:

xml.setProperty "SelectionLanguage", "XPath"

您的 XML 使用两个命名空间,如果您想要在两个命名空间中使用 select 元素,则必须使用不同的前缀声明它们。所以用

set xml = CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage", "XPath"
xml.setProperty "SelectionNamespaces", "xmlns:jb='urn:jboss:domain:1.2' xmlns:jl='urn:jboss:domain:logging:1.1'"
slog4j = "WARN"

err.clear
on error resume next
xml.load (WScript.arguments(0))
if (err.number = 0) then
xml.selectSingleNode("//jb:profile/jl:subsystem/jl:root-logger/jl:level/@name").text = slog4j
strResult = xml.save(WScript.arguments(0))
end if

我更改了属性。