Solved

Including XML Schema in XML File with MSXML4 DOM

Posted on 2004-10-06
2
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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 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)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

736 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