Xml – How to delete a specific node within a XML file by using vbscript

vbscriptxmlxpath

I am having the problem that I cannot select a specific XML node which needs to be deleted. I have already tried to select the node by using the XPath which works fine for some XML files but I cannot figure out the correct XPath for a node in a more complex file.

Does anybody know a freeware tool which can load a XML file so that the user can select a specific node and receives the accurate XPath without having an enumeration in the path?

/root/anything[2] <– unfortunatly I cannot use such a statement because the number of the element might change. I need an expression that is based on an attribute.

In case that there is no freeware tool for this operation, does anybody know another way how I can select the needed node?

XML Sample:

Root Node: SmsFormData

Attributes: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" FormatVersion="1.0" xmlns="http://schemas.microsoft.com/SystemsManagementServer/2005/03/ConsoleFramework"

Child node: Form

Attributes: Id="some GUID" CustomData="Some data" FormType="some type" ForceRefresh="false"

Child/Child node: Pages

Child/Child/Child node: Page

Attributes: VendorId="VendorName" Id="some GUID" Assembly="dll File name" Namespace="some Namespace" Type="some Type" HelpID="">

My xPath expression to select this specific page would now be:

xPath = /SmsFormData/Form/Pages/Page[@Id="some Guid"]

To do the selection I am using the following vbscript code:

Set objDOM = CreateObject("Msxml2.DOMDocument.4.0") 
objDOM.async = false            
objDOM.load(file)       
set objNode = objDOM.selectSingleNode(xPath)

The problem is now that the objNode object is empty. The node is not selected, but why?

Best Answer

This is a default namespace issue. Try including the following code after you load in the XML:

objDom.SetProperty "SelectionNamespaces", "xmlns:cf=""http://schemas.microsoft.com/SystemsManagementServer/2005/03/ConsoleFramework"""

You then use this cf prefix in your XPath eg:

objDom.SelectSingleNode("/cf:SmsFormData/cf:Form/cf:Pages/cf:Page[@Id='Some Guid']")

Whilst this may seem a bit quirky, it is intentional behaviour. Take a look at http://support.microsoft.com/kb/288147 for more information, and you may find http://msdn.microsoft.com/en-us/library/ms950779.aspx useful as well.

Related Topic