0

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

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>

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>

This is the validation code:

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);

Why the xml file is deteced as a valid xml?

Thanks

Community
  • 1
  • 1
Roi Bar
  • 105
  • 11

1 Answers1

0

I think you may have the same issue as here:

XDocument.Validate is always successful

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.

Community
  • 1
  • 1
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
  • And what can I do if my xml doesn't have namespace? – Roi Bar Apr 07 '16 at 10:11
  • If the schema has a target namespace on all elements, and the XML has no namespaces, then the namespaces of the schema (whatever they are) and the namespace of the XML instance (none) don't match. In a good validation interface for XSD there ought to be a way to specify what element declaration or type definition you want to validate against, and there ought to be a way to distinguish between 'valid' and 'validity not known' as results. – C. M. Sperberg-McQueen Apr 08 '16 at 00:39