Solved

Including XML Schema in XML File with MSXML4 DOM

Posted on 2004-10-06
2
385 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Question on web subscription for apps 3 74
Lines Not Lining Up 2 51
Image decoding from Camera 3 45
Stupid git question 2 21
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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 …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now