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!

XML Namespace using MSXML2.DOMDocument40 1

Status
Not open for further replies.

sarad1978

Technical User
Jan 16, 2003
9
US
I'm new so forgive this question please.
I'm creating a new XML document using the MSXML Dom object.

Code:
Set xmldoc = New MSXML2.DOMDocument40
xmldoc.async = False
xmldoc.validateOnParse = False
xmldoc.resolveExternals = False
xmldoc.preserveWhiteSpace = True

Set ProcInstr = xmldoc.createProcessingInstruction("xml", "version='1.0'")
xmldoc.appendChild ProcInstr
Set ProcInstr = Nothing

'Create the root element with 3 attributes
Set rootElement = xmldoc.createElement("marketlive")
Set xmldoc.documentElement = rootElement

Set rootatt = xmldoc.createAttribute("xmlns")
rootatt.Text = "rootElement.Attributes.setNamedItem rootatt

Set rootatt1 = xmldoc.createAttribute("xmlns:xsi")
rootatt1.Text = "rootElement.Attributes.setNamedItem rootatt1

Set rootatt2 = xmldoc.createAttribute("xsi:schemaLocation")
rootatt2.Text = " \\fileserver1\company\MSOFFI~1\access\478HOL~1\MarketLive_schema\MarketLive.xsd"
rootElement.Attributes.setNamedItem rootatt2


Set aElement = xmldoc.createElement("import")

Set aElement2 = xmldoc.createElement("import")
rootElement.appendChild aElement

my problem is when the file is created the "import" tag has an attribute called xmlns="" so when I validate the file against the schema it fails.

I know it has something to do with namesspace but I'm very new and still trying to get a handle on it.

Output:
<?xml version="1.0" ?>
- <marketlive xmlns=" xmlns:xsi=" xsi:schemaLocation=" \\fileserver1\company\MSOFFI~1\access\478HOL~1\MarketLive_schema\MarketLive.xsd">
- <import xmlns="">

I would really appreciate any help anyone can give.


Thanks,
Sara
 
Sara,

Instead of this brute-force approach, you must use namespace-aware methods.

Microsoft said:
You can only create a namespace-qualified element using the createNode method of the DOMDocument object.

Google [google]MSXML createNode site:microsoft.com[/google] should bring up a link to the documentation.

Tom Morrison
 
With parent element having _default_ namespace, you do have to take additional care to make known inheritence of namespace is intended.

>Set aElement = xmldoc.createElement("import")
[tt]Set aElement = xmldoc.createNode(1,"import",rootatt.Text) '1 for element nodeType[/tt]
 
Thanks Tsuji for the example and thanks k5tm for the documentation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top