问题
For the following XML fragment:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
What do the xmlns
, xmlns:xsi
, and xsi:schemaLocation
attributes exactly mean? How are they related? What's the :
for?
And there are 2 URLs in the xsi:schemaLocation=
- http://maven.apache.org/POM/4.0.0 (it happens to be the same as
xmlns
but it gives a 404 error when visiting.) - http://maven.apache.org/xsd/maven-4.0.0.xsd (this is an actual XSD doc)
If 1 doesn't exist, why still put it there?
回答1:
Namespace related attributes in XML and XML Schema (XSD)
xmlns is part of the W3C Namespaces in XML Recommendation:
The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/.
In your example, it declares that
http://maven.apache.org/POM/4.0.0
is the default namespace for the elements in your Maven project.xmlns:xsi declares a standard namespace prefix (
xsi
) for a core namespace used in XSD:http://www.w3.org/2001/XMLSchema-instance
XML Schema: Structures also defines several attributes for direct use in any XML documents. These attributes are in a different namespace, which has the namespace name http://www.w3.org/2001/XMLSchema-instance. For brevity, the text and examples in this specification use the prefix xsi: to stand for this latter namespace; in practice, any prefix can be used.
In your example, it declares the conventional binding of the
xsi
namespace prefix tohttp://www.w3.org/2001/XMLSchema-instance
, which properly sets up the use of the following attributes:xsi:type allows an XML instance to associate element type information directly rather than through an XSD. See How to restrict the value of an XML element using xsi:type in XSD?
In your example,
xsi:type
is not used; included here for completeness regardingxsi
.xsi:nil allows an empty element to be considered to be valid when the XSD might not otherwise have allowed it.
In your example,
xsi:nil
is not used; included here for completeness regardingxsi
.xsi:schemaLocation and xsi:noNamespaceSchemaLocation provide hints to the XML processor as to how to associate an XSD with an XML document. Use
xsi:schemaLocation
when there is a namespace; usexsi:noNamespaceSchemaLocation
when there is no namespace.In your example, there is a namespace, so you properly use
xsi:schemaLocation
, whose values are space-separated pairs of namespace and XSD-location-URI. Your example uses the namespace,http://maven.apache.org/POM/4.0.0
, and namespaces are lexical naming constructs that need not be retrivable. Your example also uses the XSD-location-URI,http://maven.apache.org/xsd/maven-4.0.0.xsd
, which is retrivable as it should be.If your example did not use a namespace, you would use
xsi:noNamespaceSchemaLocation
, whose value is a single XSD-location-URI that hints to the location of the intended XSD and which should be retrievable.
targetNamespace is an attribute on the
xs:schema
root element of an XSD which specifies the namespace of the root element of the XML document instances the XSD is intended to govern. It must match the default or explicit namespace of those XML documents' root elements.
回答2:
xmlns
defines default namespace, which states, that all nodes within project node and without an namespace-alias will be in http://maven.apache.org/POM/4.0.0
namespace by default.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
defines namespace - http://www.w3.org/2001/XMLSchema-instance
, and gives it a new alias - xsi
xsi:schemaLocation
is an attribute schemaLocation
of "http://www.w3.org/2001/XMLSchema-instance"
namespace. It contains pairs of values - namespace URI and schema location link for xsd-schema file of that namespace. It can contain many pairs of values - one xsd file for every defined namespace URI. That means link http://maven.apache.org/xsd/maven-4.0.0.xsd
contains xsd schema with definition of http://maven.apache.org/POM/4.0.0
namespace.
来源:https://stackoverflow.com/questions/34202967/xmlns-xmlnsxsi-xsischemalocation-and-targetnamespace