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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to STOP parent attr "xmlns" from repeating on child nodes

Status
Not open for further replies.

srfish

Programmer
Oct 29, 2003
7
GB
How can I STOP a parent attribute "xmlns" from repeating itself onto the child nodes?

I am using MSXML4 DOM to create a xml file. The ouput from my sample code looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type='text/xml' href='test.xsl'?>
<!-- Some comment.-->
<GovTalkMessage xmlns=" <node1 xmlns="">2.0</<node1>
<node2 xmlns="">
<![CDATA[ <some mark-up text> ]]>
</node2>
<node3 xmlns="">
<subNode1 />
<subNode2 />
<subNode3 />
</node3>
</GovTalkMessage>

See how the attribute "xmlns=" is repeated onto the child node1, node2 and node3 even though I have not set it.

The following code is a sample code that porduced the above output.

Private Function CreateDOM()
Dim dom
Set dom = New DOMDocument40
dom.async = False
dom.validateOnParse = False
dom.resolveExternals = False
dom.preserveWhiteSpace = True
Set CreateDOM = dom
End Function

Private Sub Form_Load()
Dim dom, node, attr
Dim namedNodeMap As IXMLDOMNamedNodeMap

On Error GoTo ErrorHandler

Set dom = CreateDOM

' Create a processing instruction targeted for xml.
Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
dom.appendChild node
Set node = Nothing

' Create a processing instruction targeted for xml-stylesheet.
Set node = dom.createProcessingInstruction("xml-stylesheet", "type='text/xml' href='test.xsl'")
dom.appendChild node
Set node = Nothing

' Create a comment for the document.
Set node = dom.createComment("Some comment.")
dom.appendChild node
Set node = Nothing

' Create the root element GovTalkMessage.
Dim root
Set root = dom.createElement("GovTalkMessage")

' Create a "xmlns" attribute for the root element and
' assign the "http..." character data as the attribute value.
Set attr = dom.createAttribute("xmlns")
attr.nodeTypedValue = " root.setAttributeNode attr
Set attr = Nothing

' Add the root element to the DOM instance.
dom.appendChild root
' Insert a newline + tab.
root.appendChild dom.createTextNode(vbNewLine + vbTab)
' Create and add more nodes to the root element just created.
' Create a text element.
Set node = dom.createElement("node1")
node.Text = "2.0"
' Add text node to the root element.
root.appendChild node
Set node = Nothing
' Add a newline plus tab.
root.appendChild dom.createTextNode(vbNewLine + vbTab)

' Create an element to hold a CDATA section.
Set node = dom.createElement("node2")
Set cd = dom.createCDATASection("<some mark-up text>")
node.appendChild cd
Set cd = Nothing
dom.documentElement.appendChild node
Set node = Nothing
' Add a newline plus tab.
root.appendChild dom.createTextNode(vbNewLine + vbTab)

' Create an element to hold three empty subelements.
Set node = dom.createElement("node3")

' Create a document fragment to be added to node3.
Set frag = dom.createDocumentFragment
' Add a newline + tab + tab.
frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
frag.appendChild dom.createElement("subNode1")
' Add a newline + tab + tab.
frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
frag.appendChild dom.createElement("subNode2")
' Add a newline + tab + tab.
frag.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
frag.appendChild dom.createElement("subNode3")
' Add a newline + tab.
frag.appendChild dom.createTextNode(vbNewLine + vbTab)
node.appendChild frag
Set frag = Nothing

root.appendChild node
' Add a newline.
root.appendChild dom.createTextNode(vbNewLine)
Set node = Nothing

' Save the XML document to a file.
dom.save App.Path + "\irEOY.xml"
Set root = Nothing
Set dom = Nothing
Exit Sub

Why does MSXML4 add the xmlns="" attribute to the child nodes? How do I stop it?

Thanks

PS Have posted this on VB forum as well so sorry for repeating it here....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top