【问题标题】:Does XMLHttpRequest check XML documents against their XSD if it exists?XMLHttpRequest 是否会根据其 XSD 检查 XML 文档(如果存在)?
【发布时间】:2011-09-29 07:48:47
【问题描述】:

如果没有,有什么方法可以检查 XML 文档与它的 XSD 是否相符?

【问题讨论】:

    标签: javascript xml ajax xsd


    【解决方案1】:

    XMLHttpRequet 是否根据其 XSD 检查 XML 文档(如果存在)?

    没有。

    XMLHttpRequest 只是一个方法名,内容不一定是 XML(这就是为什么它通常与 JSON 和序列化表单一起使用)。 XML 解析器通常只检查 XML 是否有效,而不检查它是否符合特定的模式或 DTD。我怀疑任何浏览器 XML 解析器都可以。

    如果要检查模式或 DTD,则需要一个 XML 验证器,例如 XMLSpy 中的验证器。正如 Harun 所发布的,您可能能够访问将进行验证的主机对象,但它很可能不会跨浏览器。

    【讨论】:

      【解决方案2】:

      用于根据 xsd 验证 xml 文件的 JavaScript 代码,

             <SCRIPT LANGUAGE="JavaScript">
      
                var strFile=path of xml file;
      
                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", "xsd path");
      
                 // 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>
      

      XML 文件,

             <?xml version="1.0"?> 
               <bookstore xmlns="generic"> 
                <book genre="autobiography"> 
                 <title>The Autobiography of Benjamin Franklin</title>                 
                  <price>89.88</price> 
               </book> 
             <book genre="novel"> 
              <title>The Confidence Man</title>             
              <price>11.99</price> 
             </book> 
            </bookstore> 
      

      XSD 文件(架构文件),

             <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="generic" elementFormDefault="qualified" targetNamespace="generic"> 
               <xsd:element name="bookstore" type="bookstoreType"/> 
               <xsd:complexType name="bookstoreType"> 
                   <xsd:sequence maxOccurs="unbounded"> 
                      <xsd:element name="book" type="bookType"/> 
                   </xsd:sequence> 
               </xsd:complexType> 
               <xsd:complexType name="bookType"> 
                 <xsd:sequence> 
                    <xsd:element name="title" type="xsd:string"/>                  
                    <xsd:element name="price" type="xsd:decimal"/> 
                 </xsd:sequence> 
               <xsd:attribute name="genre" type="xsd:string"/> 
               </xsd:complexType>             
          </xsd:schema> 
      

      希望这会有所帮助..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-25
        • 2020-06-08
        • 1970-01-01
        • 2016-10-19
        • 2019-09-16
        • 2012-01-08
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多