[Excel] XML Attribute Duplicated
Im encountering a strange issue where I am not adding an attribute to my XML tag but it is appearing anyway.
vb Code:
Option Explicit
' This procedure creates XML document
' Requires msxml.dll (Go to Project --> References and
' and choose Microsoft XML version 2.0, or whatever the
' current version you have installed)
Private Sub Create_XML()
Dim objDom As DOMDocument
Dim objProcInst As IXMLDOMProcessingInstruction
Dim objRootElem As IXMLDOMElement
Dim objHeaderElem As IXMLDOMElement
Dim objTargetElem As IXMLDOMElement
Dim objTradePosElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objMemberRel As IXMLDOMAttribute
Dim objMemberName As IXMLDOMElement
Dim objAtt As IXMLDOMAttribute
Set objDom = New DOMDocument
Set objProcInst = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='yes'")
objDom.appendChild objProcInst
' Creates root element
Set objRootElem = objDom.createElement("riskImport")
objDom.appendChild objRootElem
' Creates NameSpace Attributes etc on to the root Element
Set objAtt = objDom.createAttribute("xmlns")
objAtt.NodeValue = "http://www.test.com/xml"
objRootElem.setAttributeNode objAtt
Set objAtt = objDom.createAttribute("schemaVersion")
objAtt.NodeValue = "1.0"
objRootElem.setAttributeNode objAtt
Set objAtt = objDom.createAttribute("importDatetime")
objAtt.NodeValue = "2009-06-05T14:20:20.661Z"
objRootElem.setAttributeNode objAtt
' Create riskDataHeader element
Set objHeaderElem = objDom.createElement("riskDataHeader")
objRootElem.appendChild objHeaderElem
Set objMemberName = objDom.createElement("portfolio")
objMemberName.Text = "myPortfolio"
objHeaderElem.appendChild objMemberName
Debug.Print objDom.XML
End Sub
This outputs the following.
Why is the xmlns attribute appearing on the riskDataHeader element and how to I prevent it from happening?
Code:
<?xml version="1.0" standalone="yes"?>
<riskImport xmlns="http://www.test.com/xml" schemaVersion="1.0" importDatetime="2009-06-05T14:20:20.661Z">
<riskDataHeader xmlns="">
<portfolio>myPortfolio</portfolio>
</riskDataHeader>
</riskImport>
Re: [Excel] XML Attribute Duplicated
Quote:
' Creates NameSpace Attributes etc on to the root Element
Set objAtt = objDom.createAttribute("xmlns")
objAtt.NodeValue = "http://www.test.com/xml"
objRootElem.setAttributeNode objAtt
you create it??
Re: [Excel] XML Attribute Duplicated
Isn't that just adding the attribute on the root element though?
Code:
<riskImport xmlns="http://www.test.com/xml" schemaVersion="1.0" importDatet.....
The problem I have is that it also appears as an attribute on EVERY element below the root as well.
What is wierder is that just before I save the file at the end, if I output objHeaderElem.xml to the immediate window it doesn't contain this attribute, but if I output objRootElem.xml to the immediate, the header info DOES include it!!
Code:
?objHeaderElem.xml
<riskDataHeader><portfolio>myPortfolio</portfolio></riskDataHeader>
?objRootElem.xml
<riskImport xmlns="http://www.test.com/xml" schemaVersion="1.0" importDatetime="2009-06-05T14:20:20.661Z">
<riskDataHeader xmlns=""><portfolio>myPortfolio</portfolio></riskDataHeader></riskImport>
Re: [Excel] XML Attribute Duplicated
Could it be that as it is a header it is added afterwards (like headers n footers in word) to the data.
So you define the headers as things you want to add to all data elements on save?
(I am guessing tho)
Re: [Excel] XML Attribute Duplicated
Hmmm, I am still not sure what is causing it.
However, I have realised that I don't have to create an attribute before setting the value.
The setAttribute seems to create one.
So now I am doing the following instead
vb Code:
objRootElem.setAttribute "xmlns", "http://www.test.com/xml"
Wierd.
Re: [Excel] XML Attribute Duplicated
very
possibly the create attribute means that the current and next child use it?
DOM
--Riskimport
--+xmlns
--+schemaversion
--+importdate
----RiskDataHeader
------Portfolio
In theory tho if it grabbed the nodes from the parent then the portfolio would also have one. The only thing I can see different is that you set a value in portfolio... could it be that it defaultly adds in nodes?
weird how it doesn't add all the nodes tho.
at least you found a way around it.