Solved

Including XML Schema in XML File with MSXML4 DOM

Posted on 2004-10-06
2
388 Views
Last Modified: 2013-11-19
Hi,

No doubt its been asked before, but I haven't been able to find a solution to this problem anywhere, so here goes.

I have an XML Schema saved in a file, lets say "schema.xsd".  I am generating the XML document that this schema will validate dynamically from within Visual Basic 6 with MXSML4.  Now, I need to put a reference to the schema onto the root element of the XML I am generating. eg:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:schema.xsd">
      --- rest of XML document ---
</root>

This needs to be done within the DOM as I generate the XML.  How do I do this?  MSDN suggests using XMLSchemaCache but that doesn't work, and I can't find any other way.

Any suggestions?
0
Comment
Question by:nintoid
2 Comments
 
LVL 26

Accepted Solution

by:
rdcpro earned 100 total points
ID: 12241922
The XMLSchemaCache is simply a way of validating XML documents against a locally cached copy of a schema (which might be better than depending on loading it from a public location specified in the XML).  But to answer your question, you use the createNode() method to create an attribute node scoped to the schema instance namespace, then add the node to the attributes collection of the root element.

Dim oAttrNode As IXMLDOMAttribute

Set oAttrNode = xmlDoc.createNode(2, "xsi:noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance")
oAttrNode.Value = "file:schema.xsd"

Set oRootNode = xmlDoc.documentElement
oRootNode.setAttributeNode oAttrNode


Note that this does not automatically cause the XML document to be validated!  That will only happen when the XML is parsed with these attributes, and the validateOnParse property is set.  You then have to check for parseErrors.

Regards,
Mike Sharp
0
 

Author Comment

by:nintoid
ID: 12246270
Thanks Mike, that worked perfectly!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question