Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change a namespace attribute value in XML schema

Status
Not open for further replies.

RickBeddoe

Programmer
Nov 18, 2008
32
US
Hello folks. I've also posted this in the XML forum.

Having a tough time trying to figure this one out.

I have a schema (xsd) file with namespaces declared in the root element as shown:


<xsd:schema xmlns:xsd=" xmlns:ci-com="
I would like to change the definition of this attribute from this:

xmlns:ci-com="
to this:

xmlns:ci-com="
I am doing this in VBA (with DOM interface) and everything I've tried kicks up an error that the attribute is read only. Here is the code:

newNode.nodeValue = newAttValue

where newNode is the root node (<xsd:schema...>) and newAttValue is a string representing the new vallue.


Any help would be appreciated.
 
In case anyone comes across this thread in the future, here's the solution:

Basically, use the 'setNamedItem' method of the Attributes property.


Function replaceAttributeValue(ByRef doc As DOMDocument60, xmlNode As IXMLDOMNode, oldAttValue As String, newAttValue As String) As IXMLDOMElement

Dim nodeLst As IXMLDOMNodeList
Dim nodeElem As IXMLDOMElement
Dim elmAttrs As IXMLDOMNamedNodeMap
Dim elmAttr As IXMLDOMAttribute
Dim newAttr As IXMLDOMAttribute
Dim replAttrValue As String
Dim ns As String, nsNodeName As String

' Get all the attributes in the node
Set elmAttrs = xmlNode.Attributes

'iterate through the attributes
For Each elmAttr In elmAttrs

If InStr(elmAttr.Value, oldAttValue) > 0 Then
'to use setNamedItem, you need a new node
'you need a namespace to make the new node
ns = elmAttr.namespaceURI

nsNodeName = elmAttr.nodeName
Set newNode = doc.createNode(NODE_ATTRIBUTE, nsNodeName, ns)
newNode.nodeValue = Replace(elmAttr.nodeValue, oldAttValue, newAttValue)
xmlNode.Attributes.setNamedItem newNode

End If
Next
Set replaceAttributeValue = xmlNode

'iterate through any childnodes
If xmlNode.hasChildNodes Then
Set nodeLst = xmlNode.selectNodes("*")
For Each nodeElem In nodeLst
Set nodeElem = replaceAttributeValue(doc, nodeElem, oldAttValue, newAttValue)
Next
End If


End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top