且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 vbscript 在现有 xml 中添加属性

更新时间:2023-12-01 14:08:46

这是一个使用 MSXML.

Dim doc
Dim pageUrl
Dim itemNode

Set doc = CreateObject("MSXML2.DOMDocument")
doc.load("test.xml")
doc.setProperty "SelectionNamespaces", "xmlns:tcm='http://www.tridion.com/ContentManager/5.0'"

Set itemNode = doc.selectSingleNode("/tcm:ListItems/tcm:Item[@ID = 'tcm:481-594343']")

Set pageUrl = doc.createAttribute("pageURL") 
pageUrl.Value = "/english/destinations_offers/destinations/asiapacific/maldives.aspx" 
itemNode.attributes.setNamedItem(pageUrl) 

应用于您提供的 XML 示例时.它产生以下输出.

When applied to the XML sample you provided. It produces the following output.

<?xml version="1.0"?>
<tcm:ListItems xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ID="tcm:481-86880-2" Managed="10682">
    <tcm:Item ID="tcm:481-594051"/>
    <tcm:Item ID="tcm:481-594088"/>
    <tcm:Item ID="tcm:481-594089"/>
    <tcm:Item ID="tcm:481-594090"/>
    <tcm:Item ID="tcm:481-594343" pageURL="/english/destinations_offers/destinations/asiapacific/maldives.aspx"/>
    <tcm:Item ID="tcm:481-594344"/>
    <tcm:Item ID="tcm:481-594578"/>
</tcm:ListItems>