Your Ad Here

Sunday, June 29, 2008

JavaScript xsd validation and examples

<SCRIPT LANGUAGE="JavaScript">
var sOutput = validateFile("sc-valid.xml");
alert(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", "sc.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");
}

</SCRIPT>
=========================

File: sc-valid.xml
-------------------
<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</x:catalog>

-----------------------------
File: sc.xsd
-----------------------------
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:b="urn:books">
<xsd:element name="catalog" type="b:CatalogData"/>
<xsd:complexType name="CatalogData">
<xsd:sequence>
<xsd:element name="book"
type="b:bookdata"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookdata">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="publish_date" type="xsd:date"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

1 comment:

S.Hossein.Asadallahi said...

hi
thanks a lot

but how can i use it in firefox?

Your Ad Here
#chitikatest=doctor