【问题标题】:Why my xml file is detected as a valid xml?为什么我的 xml 文件被检测为有效的 xml?
【发布时间】:2016-04-06 23:30:05
【问题描述】:

我想为 MS office Docx 的 [Content_Types].xml 文件 (Where can I find the XSDs of DOCX XML files?) 找到一个 xsd 文件 我得到了答案,但我发现由于某种原因,这个 xsd 导致我所有的 xml 文件都通过了验证测试

这是我的 xml 文件:

<?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 的架构):

<?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?

谢谢

【问题讨论】:

  • 你的 XML 有什么问题?
  • 它的结构不像[Content_Types].xml

标签: c# xml xsd


【解决方案1】:

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

XDocument.Validate is always successful

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

【讨论】:

  • 如果我的xml没有命名空间怎么办?
  • 如果架构在所有元素上都有一个目标命名空间,并且 XML 没有命名空间,那么架构的命名空间(无论它们是什么)和 XML 实例的命名空间(无)都没有匹配。在一个好的 XSD 验证接口中,应该有一种方法可以指定要验证的元素声明或类型定义,并且应该有一种方法来区分结果中的“有效”和“未知的有效性”。
猜你喜欢
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
相关资源
最近更新 更多