问题
So before we write the XML Schema, a lot of tutorials use this:
<?xml version='1.0'?>
or
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
My question is, what is this part for? What is that website specifically and why do we use it? Are there other methods to do this?
If it helps, I am doing this to convert an excel worksheet into XML.
回答1:
XML Declaration
<?xml version='1.0'?>
is an XML declaration and is not particular to XSDs but to XML documents in general.
[Definition: XML documents should begin with an XML declaration which specifies the version of XML being used.]
As an XSD is an XML document, it may also have an XML declaration.
Here is the BNF of an XML declaration (XMLDecl
) with links to the definitions of its constituent parts:
XMLDecl ::= '<?xml'
VersionInfo EncodingDecl?
SDDecl?
S? '?>'
Note: Only one XML declaration is permitted in well-formed XML, and it must be at the top if anywhere. If you violate this requirement, you'll see an error such as
The processing instruction target matching "[xX][mM][lL]" is not allowed.
and you'll have to fix the problem before your XML will be well-formed.
XML Schema Instance Namespace
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
is a namespace declaration for the special XML Schema instance namespace. As a namespace URI, it's purpose is to facilitate control of grouping of component name. An XML namespace URI does not have to be retrievable.
See also
- What are XML namespaces for regarding the purpose of namespaces in general.
- Schema-Related Markup in Documents Being Validated for XSD attributes
that use
xsi
in particular. (xsi:type
,xsi:nil
,xsi:schemaLocation
, andxsi:noNamespaceSchemaLocation
)
回答2:
XML declaration. See https://www.w3.org/TR/2006/REC-xml-20060816/#sec-prolog-dtd for more information.
回答3:
<?xml version='1.0'?>
1.0 is the current version of XML. And the number might change for future versions. This is a mandatory field and it indicates which version of XML standard this file conforms to.
encoding="UTF-8"
This mean the file is encoded in UTF-8. This is optional since this is the default character encoding in XML.
standalone="yes"
standalone indicates whether if the current XML document depends on an external markup declaration. This is also optional.
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This is the XML Schema Instance namespace. After having this declared you can use attributes such as schemaLocation.
See similar answer: Is xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" a special case in XML?
来源:https://stackoverflow.com/questions/35835870/what-is-on-the-first-line-of-an-xml-document