简体   繁体   中英

Is it valid to include XSD schema definition in XML file?

When i generate a .resx file with Visual Studio, which is an xml resource file, then the XSD schema is included in the same file. (see example) Other tools are able to interprete this file and it seems to be "valid". Nevertheless i can not find any information on w3schools that this is valid. Theory teaches to write an xsd file and reference it in the xml file. As well for validation normally an xsd file and an xml file is provided.

Is it valid to define the xsd schema in the xml file like in the given example? If yes, where does it apply? To the node it is included in? If you could provide a link with more explanation, it would be as well appreciated.

<?xml version="1.0" encoding="utf-8"?>
<root>
    <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:element name="root" msdata:IsDataSet="true">
            <xsd:complexType>
                <xsd:choice maxOccurs="unbounded">
                    <xsd:element name="data">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
                            </xsd:sequence>
                            <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
                            <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
                            <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="resheader">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                            </xsd:sequence>
                            <xsd:attribute name="name" type="xsd:string" use="required" />
                        </xsd:complexType>
                    </xsd:element>
                </xsd:choice>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
    </resheader>
    <resheader name="version">
        <value>1.3</value>
    </resheader>
    <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>
    <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>
  <data name="Key">
    <value>Value</value>
  </data>
</root>

Edit in response to adrianos comments: I should have reviewed the W3C website indeed. It's just a lot more difficult to read and understand... I did now, but in that short time i can not get the full picture. What i found:

I do not see any hint yet, that including a schema should be considered as valid. It seems practical to me but it's like mixing data and definition.

Given all your references, I am almost sure that you know the difference between well-formed XML and valid XML. So, when you ask "is it valid?", what do you have in mind? Valid according to what?

Your sample may be valid, but that would be as per some Microsoft spec - other tools are most likely drawing blanks on this one.

If you're thinking more about an arbitrary XML, for which you have an XSD, and if you're asking if there is a "standard" way to include that XSD along with the XML , and then magically have some third-party XSD-aware XML processor pick that XSD and use it to validate the rest of the XML... then no, I don't think that there's a standard for it.

Just like your example, if you want to define your own XML format that would inline the XSD, there's nothing in the XML or the XSD spec that would not allow you do it; no other tool will pick it up, but it could work for your proprietary application. I would point out that there are some data formats that include the schema along with the data for reasons related to interoperability, versioning, etc.

The standardized way to provide "hints" as to what XSD to use for validation, are indeed the XML Schema Instance attributes schemaLocation and noNamespaceSchemaLocation you referred to; yet, the spec also says that these are just that, "hints", a processor is free to ignore them - so even if valid, they might not work.

Example 1 (no namespace):

<person xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://adventure-works.com/schemas/person.xsd">
   <name>John</name>
   <height>59</height>
</person>

Example 2 (multiple namespaces, multiple schemas):

<p:Person xmlns:p="http://contoso.com/People" xmlns:v="http://contoso.com /Vehicles" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=
     "http://contoso.com/People 
      http://contoso.com/schemas/people.xsd 
      http://contoso.com/schemas/Vehicles
      http://contoso.com/schemas/vehicles.xsd
      http://contoso.com/schemas/People
      http://contoso.com/schemas/people.xsd">
    <name>John</name>
    <age>28</age>
    <height>59</height>
    <v:Vehicle>
        <color>Red</color>
        <wheels>4</wheels>
        <seats>2</seats>
    </v:Vehicle>
</p:Person>

Almost any XML editor has its own supported mechanism to associate XSDs with your XML, from individual properties to XSD catalogs and, of course, support for the XSI attributes.

"Inlining" XSD in XML is tricky business; if you involve multiple XML instances then you have maintenance issues, etc. If for some reason one would really need something like this, I would use XInclude for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM