?
Solved

Including XML Schema in XML File with MSXML4 DOM

Posted on 2004-10-06
2
Medium Priority
?
411 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 400 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

771 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