how to add a attribute in existing xml using vbscript

做~自己de王妃 提交于 2019-12-22 18:34:35

问题


I have below xml and I am using VBSript to generate it.

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

Now I have got a pageURL (/english/destinations_offers/destinations/asiapacific/maldives.aspx), this will be shown after matching the ID for example below pseudocode

From above XML ID will be matched and then we will add the pageURL attribute to above xml. So the output will come as below:

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

Please suggest using VBSCript

Thanks.


回答1:


Here's an example using 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) 

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>


来源:https://stackoverflow.com/questions/3499734/how-to-add-a-attribute-in-existing-xml-using-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!