为什么我的 xml 文件被检测为有效的 xml? [英] Why my xml file is detected as a valid xml?

查看:34
本文介绍了为什么我的 xml 文件被检测为有效的 xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 MS office Docx 的 [Content_Types].xml 文件找到一个 xsd 文件(在哪里可以找到 DOCX XML 文件的 XSD?)我得到了答案,但我发现由于某种原因,这个 xsd 导致我所有的 xml 文件都通过了验证测试

I wanted to find a xsd file for MS office Docx's [Content_Types].xml file (Where can I find the XSDs of DOCX XML files?) I got an answer, but I've found that for some reason this xsd cause all of my xml files to pass validation test

这是我的 xml 文件:

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<Export_toLab Sending_Site="3">
  <Tracker Tracker_ID="55" Booklet_Type="3">
    <Booklet Booklet_ID="542"/>
  </Tracker>
</Export_toLab>

这是 xsd 文件(MS office Docx 的 [Content_Types].xml 的架构):

This is the xsd file (schema of MS office Docx's [Content_Types].xml):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns="http://schemas.openxmlformats.org/package/2006/content-types"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://schemas.openxmlformats.org/package/2006/content-types"
  elementFormDefault="qualified" attributeFormDefault="unqualified" blockDefault="#all">
    <xs:element name="Types" type="CT_Types"/>
    <xs:element name="Default" type="CT_Default"/>
    <xs:element name="Override" type="CT_Override"/>
    <xs:complexType name="CT_Types">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="Default"/>
            <xs:element ref="Override"/>
        </xs:choice>
    </xs:complexType>
    <xs:complexType name="CT_Default">
        <xs:attribute name="Extension" type="ST_Extension" use="required"/>
        <xs:attribute name="ContentType" type="ST_ContentType" use="required"/>
    </xs:complexType>
    <xs:complexType name="CT_Override">
        <xs:attribute name="ContentType" type="ST_ContentType" use="required"/>
        <xs:attribute name="PartName" type="xs:anyURI" use="required"/>
    </xs:complexType>
    <xs:simpleType name="ST_ContentType">
        <xs:restriction base="xs:string">
            <xs:pattern
        value="(((([\p{IsBasicLatin}-[\p{Cc}&#127;\(\)&lt;&gt;@,;:\\&quot;/\[\]\?=\{\}\s\t]])+))/((([\p{IsBasicLatin}-[\p{Cc}&#127;\(\)&lt;&gt;@,;:\\&quot;/\[\]\?=\{\}\s\t]])+))((\s+)*;(\s+)*(((([\p{IsBasicLatin}-[\p{Cc}&#127;\(\)&lt;&gt;@,;:\\&quot;/\[\]\?=\{\}\s\t]])+))=((([\p{IsBasicLatin}-[\p{Cc}&#127;\(\)&lt;&gt;@,;:\\&quot;/\[\]\?=\{\}\s\t]])+)|(&quot;(([\p{IsLatin-1Supplement}\p{IsBasicLatin}-[\p{Cc}&#127;&quot;\n\r]]|(\s+))|(\\[\p{IsBasicLatin}]))*&quot;))))*)"
      />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ST_Extension">
        <xs:restriction base="xs:string">
            <xs:pattern
        value="([!$&amp;'\(\)\*\+,:=]|(%[0-9a-fA-F][0-9a-fA-F])|[:@]|[a-zA-Z0-9\-_~])+"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

这是验证码:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://schemas.openxmlformats.org/package/2006/content-types", "opc-contentTypes.xsd");

XDocument doc = XDocument.Load("MyFile.xml");
string msg = "";
doc.Validate(schemas, (o, e) => {msg += e.Message + Environment.NewLine;});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);

为什么 xml 文件被检测为有效的 xml?

Why the xml file is deteced as a valid xml?

谢谢

推荐答案

我想你可能和这里有同样的问题:

I think you may have the same issue as here:

XDocument.Validate 总是成功

总而言之,validate 方法不会为它不知道的节点引发错误.在验证 xml 的命名空间是否与架构相同之前,您应该简单地检查一下 - 如果不是,则它肯定是失败的,因此无需验证.

To summarise, the validate method does not raise an error for the nodes it does not know about. You should simply check before validating that the namespace of the xml is the same as the schema - if not its definitely a fail so no need to validate.

这篇关于为什么我的 xml 文件被检测为有效的 xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆