MSXML (and DOM) does not provide any direct way of specifying the schemaLocation. The workaround is to add schemaLocation nodes like any other attribute nodes.
Visual Basic example that uses MSXML 4.0 DOM to create an XML document and specify schemaLocation:
Dim objNewXMLDoc As New MSXML2.DOMDocument40
Dim rootElement As IXMLDOMElement
objNewXMLDoc.appendChild _
objNewXMLDoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
Set objNewXMLDoc.documentElement = _
objNewXMLDoc.createNode(NODE_ELEMENT, "x:doc", "http://xsdtesting")
Set rootElement = objNewXMLDoc.documentElement
rootElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
rootElement.setAttribute "xsi:schemaLocation", "http://xsdtesting 1.xsd"
objNewXMLDoc.save "c:\1.xml"
Visual Basic example that uses MXXMLWriter40 to create an XML document and specify the schemaLocation along with the root node:
Dim objXMLWriter As New MSXML2.MXXMLWriter40
Dim contHandler As MSXML2.IVBSAXContentHandler
Dim attrbs As New MSXML2.SAXAttributes40
Set contHandler = objXMLWriter
objXMLWriter.indent = True
objXMLWriter.omitXMLDeclaration = False
objXMLWriter.version = "1.0"
objXMLWriter.encoding = "utf-8"
objXMLWriter.standalone = False
contHandler.startDocument
attrbs.addAttribute "", "x", "xmlns:x", "", "http://xsdtesting"
attrbs.addAttribute "", "xsi", "xmlns:xsi", "", "http://www.w3.org/2001/XMLSchema-instance"
attrbs.addAttribute "", "schemaLocation", "xsi:schemaLocation", "", "http://xsdtesting 1.xsd"
contHandler.startElement "http://xsdtesting", "doc", "x:doc", attrbs
contHandler.endElement "http://xsdtesting", "doc", "x:doc"
contHandler.endDocument
MsgBox objXMLWriter.output
The above example create the following XML:
<?xml version="1.0"?>
<x:doc xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:x='http://xsdtesting'
xsi:schemaLocation='http://xsdtesting 1.xsd'>
/>
|