2010-08-17 74 views
1

我有下面的xml,我正在使用VBSript來生成它。如何使用vbscript在現有的xml中添加屬性

<?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"/> 
    <tcm:Item ID="tcm:481-594344"/> 
    <tcm:Item ID="tcm:481-594578"/> 
</tcm:ListItems> 

現在我有一個PAGEURL(/english/destinations_offers/destinations/asiapacific/maldives.aspx),這將匹配ID例如低於僞

從以上XML ID將是後顯示匹配,然後我們將在上面的xml中添加屬性。所以輸出會如下:

<?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> 

請建議使用VBScript

感謝。

回答

2

下面是使用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樣本。它產生以下輸出。

<?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> 
+0

非常感謝Garett,我在itemNode.attributes.setNamedItem(pageUrl)上得到了對象所需的錯誤,請你給我建議! – 2010-08-17 07:37:27

+0

它看起來像itemNode沒有定義。我編輯了這個例子。 – Garett 2010-08-17 12:22:16

相關問題