SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Validate a XML with a schema XSD

    Hello, I have a code that validates a XML. Now I would like to add a validation against a schema XSD. I searched for documentation about this, but I have not found. Can you help me?

    This is my code in JScript

    function LoadXMLFromFile(xmlPath)
    {
    Doc = Sys.OleObject("Msxml2.DOMDocument.6.0");
    Doc.async = false;

    Doc.schemas = ???????;
    Doc.load(xmlPath);

    if(Doc.parseError.errorCode != 0)
    {
    var s = "Reason: " + Doc.parseError.reason + " " +
    "Line:\t" + aqConvert.VarToStr(Doc.parseError.line) + " ." +
    "Pos:\t" + aqConvert.VarToStr(Doc.parseError.linePos) + " ." +
    "Source:\t" + Doc.parseError.srcText;

    Log.Error("Cannot parse the document. " + s);
    return;
    }

    return Doc;
    }

  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    52
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Validate a XML with a schema XSD

    Hi,

    Try using the following script to accomplish your task:

    <font class="small">Code:</font><hr /><pre>
    function Test() {
    var sOutput = validateFile(path_to_your_xml_file);
    }

    function validateFile(strFile) {
    //Create a schema cache and add your XSD file to it
    var xs = new ActiveXObject("MSXML2.XMLSchemaCache.4.0");
    xs.add("", path_to_your_xsd_file);
    //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 your XML file 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) {
    ShowMessage("Validation failed on " + strFile +
    "\nReason: " + xd.parseError.reason +
    "\nSource: " + xd.parseError.srcText +
    "\nLine: " + xd.parseError.line + "\n");
    }
    else {
    ShowMessage("Validation succeeded for " + strFile +
    xd.xml + "\n");
    }
    }
    </pre><hr />

    I hope this helps.
    Tanya,
    SmartBear Technical Community Manager, Testing Tools

    Interested in more, visit our SmartBear community

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 11.54%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 07:51 PM.

Copyright BetaSoft Inc.