validate XML with xsd via MSXML
///////////////////////empty_element.js
// create XML : <ParticipantObjectDescription><Accession Number=""/></ParticipantObjectDescription>
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var root;
var newElem;
xmlDoc.async = false;
xmlDoc.loadXML("<ParticipantObjectDescription></ParticipantObjectDescription>");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
root = xmlDoc.documentElement;
newElem = xmlDoc.createElement("Accession");
root.appendChild(newElem);
var attributes = newElem.attributes;
var number = xmlDoc.createAttribute("Number");
//number.value = "";
attributes.setNamedItem(number);
//WScript.Echo(root.xml);
}
/////validate xml by xsd
if(false)
{
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var xsdCache = new ActiveXObject("Msxml2.XMLSchemaCache.4.0");
xsdCache.add(" ","empty_element.xsd");
xmlDoc.async = false;
xmlDoc.schemas = xsdCache;
xmlDoc.load("empty_element.xml");
var err = xmlDoc.validate();
if (err.errorCode == 0){
WScript.Echo("Document is valid");
}else{
WScript.Echo("Validation error:" + err.reason);
}
}
var sOutput = validateFile("empty_element.xml");
//sOutput = sOutput + validateFile("sc-notValid.xml");
WScript.Echo(sOutput);
function validateFile(strFile)
{
// Create a schema cache and add books.xsd to it.
var xs = new ActiveXObject("MSXML2.XMLSchemaCache.4.0");
xs.add("urn:books", "empty_element.xsd");
// Create an XML DOMDocument object.
var xd = new ActiveXObject("MSXML2.DOMDocument.4.0");
// Assign the schema cache to the DOMDocument's
// schemas collection.
xd.schemas = xs;
// Load books.xml as the DOM document.
xd.async = false;
xd.validateOnParse = true;
xd.resolveExternals = true;
xd.load(strFile);
// Return validation results in message to the user.
if (xd.parseError.errorCode != 0)
{
return("Validation failed on " + strFile +
"\n=====================" +
"\nReason: " + xd.parseError.reason +
"\nSource: " + xd.parseError.srcText +
"\nLine: " + xd.parseError.line + "\n");
}
else
return("Validation succeeded for " + strFile +
"\n======================\n" +
xd.xml + "\n");
}
////////////////empty_element.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ParticipantObjectDescription" type="ParticipantObjectDescriptionType"/>
<xsd:complexType name="ParticipantObjectDescriptionType">
<xsd:sequence>
<xsd:element name="Accession" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="Number" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
////////////////////empty_element.xml
<?xml version="1.0"?>
<ParticipantObjectDescription>
<Accession Number=""/>
<Accession Number="111"/>
<Accession/>
</ParticipantObjectDescription>
// create XML : <ParticipantObjectDescription><Accession Number=""/></ParticipantObjectDescription>
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var root;
var newElem;
xmlDoc.async = false;
xmlDoc.loadXML("<ParticipantObjectDescription></ParticipantObjectDescription>");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
root = xmlDoc.documentElement;
newElem = xmlDoc.createElement("Accession");
root.appendChild(newElem);
var attributes = newElem.attributes;
var number = xmlDoc.createAttribute("Number");
//number.value = "";
attributes.setNamedItem(number);
//WScript.Echo(root.xml);
}
/////validate xml by xsd
if(false)
{
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var xsdCache = new ActiveXObject("Msxml2.XMLSchemaCache.4.0");
xsdCache.add(" ","empty_element.xsd");
xmlDoc.async = false;
xmlDoc.schemas = xsdCache;
xmlDoc.load("empty_element.xml");
var err = xmlDoc.validate();
if (err.errorCode == 0){
WScript.Echo("Document is valid");
}else{
WScript.Echo("Validation error:" + err.reason);
}
}
var sOutput = validateFile("empty_element.xml");
//sOutput = sOutput + validateFile("sc-notValid.xml");
WScript.Echo(sOutput);
function validateFile(strFile)
{
// Create a schema cache and add books.xsd to it.
var xs = new ActiveXObject("MSXML2.XMLSchemaCache.4.0");
xs.add("urn:books", "empty_element.xsd");
// Create an XML DOMDocument object.
var xd = new ActiveXObject("MSXML2.DOMDocument.4.0");
// Assign the schema cache to the DOMDocument's
// schemas collection.
xd.schemas = xs;
// Load books.xml as the DOM document.
xd.async = false;
xd.validateOnParse = true;
xd.resolveExternals = true;
xd.load(strFile);
// Return validation results in message to the user.
if (xd.parseError.errorCode != 0)
{
return("Validation failed on " + strFile +
"\n=====================" +
"\nReason: " + xd.parseError.reason +
"\nSource: " + xd.parseError.srcText +
"\nLine: " + xd.parseError.line + "\n");
}
else
return("Validation succeeded for " + strFile +
"\n======================\n" +
xd.xml + "\n");
}
////////////////empty_element.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ParticipantObjectDescription" type="ParticipantObjectDescriptionType"/>
<xsd:complexType name="ParticipantObjectDescriptionType">
<xsd:sequence>
<xsd:element name="Accession" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="Number" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
////////////////////empty_element.xml
<?xml version="1.0"?>
<ParticipantObjectDescription>
<Accession Number=""/>
<Accession Number="111"/>
<Accession/>
</ParticipantObjectDescription>