开发者

Xml Schema namespace

1) I realize namespaces are a means of differentiating between different schemas/vocabularies specified using XML Schema, but I don’t understand why it’s a good idea that a namespace ( for which the schema is developing a vocabulary ) is specified within the schema itself ( via targetNamespace attribute ).

a) Wouldn’t it be better if we were able to instead associate particular namespace with particular vocabulary/schema within instance documents? That way those writing instance documents would have a complete freedom to associate a schema with whichever namespace name they’d desire?!

b) Only benefit I see in the schema specifying a target namespace is that its creators have an option to put a document at the end of namespace that can describe the elements of that namespace ( assuming schema uses URL for a namespace ). Are there also other benefits?

2) If a schema has no targetNamespace, you must refer ( within instance document ) to the particular开发者_JS百科 schema using the noNamespaceSchemaLocation attribute instead of schemaLocation attribute.

Wouldn’t it be simpler if instead document instance would just specify the location of the schema and let the XML Schema validator figure out whether or not schema specifies a targetNamespace?

thank you


I know only one definition of XML Namespaces and one definition of the definition of the XML Schema also on http://www.w3.org. Because XML Schema is very complex one decided the standard in three parts XML Schema Part 0: Primer Second Edition, XML Schema Part 1: Structures Second Edition and XML Schema Part 2: Datatypes Second Edition.

If you know more definition of XML Namespaces and XML Schema (more "vocabularies") please post a link to the definition which you mean.

The meaning of the targetNamespace in the root of any schema is very easy to clear. XML is not a language. It is a meta-language. XML Schema helps us to define one language. We have to give an unique name to the language (the schema) which we define. It is the targetNamespace of the schema. So it the some document should be interpret as the document written in US English language it can be not quite correct in GB English.

So is the targetNamespace in a schema is unique and inside of XML file one declare the namespace of an element it means that we define in the way the exact language (and the dialect) in which the document is written. In the way you say also very exactly about the context in which the corresponding name should be interpret.

If you define a schema without the targetNamespace in means that the corresponding elements and attributes from XML document must also belong to "no namespace". The usage of noNamespaceSchemaLocation attribute or schemaLocation attribute is not mandatory. So you you must not refer the schema in the XML document. In the case the reader of the XML document should know from the corresponding context which schema you mean.

At the end of your question you ask

Wouldn’t it be simpler if instead document instance would just specify the location of XML Schema and let the Xml Schema validator figure out whether or not schema specifies a targetNamespace?

But the value of the noNamespaceSchemaLocation attribute is exactly the path to the XSD file which define the schema used. The value of the schemaLocation attribute is the namespace and the the path to the XSD file divided with the blank. So the attributes already do what you suggest.


Your idea for "late binding" of namespaces is not bad, but currently part of the purpose of the namespace is to allow a processor to figure out which schema to use.

In that process, the "schemaLocation" attribute is just a hint, and many times is not useful at all.

Consider the following scenario:

  • I am receiving XML messages on JMS
  • I have several possible schemas that may need to be applied; hardcoding URLs to the schema into the XML documents themselves doesn't work, as I cannot move environment in this case.
  • Processor uses the namespace of elements in the XML file to find a schema in an in-memory schema cache.

In this scenario, you could say "let the application maintain a mapping from namespace to schema, there is still no reason for the namespace to be specified inside the schema itself"; unfortunately this leads to problems when a schema mixes multiple namespaces with imports. Depending on the XML file you choose to you, you would create a name clash in the schema.

Hope this makes sense.


The benefit of individual schema (or "vocabulary" in your words) specifying the target namespace is that it allows for schemas to reuse schemas from other namespaces.

The most commonly reused schema is http://www.w3.org/2001/XMLSchema, which defines built-in types like xs:int and xs:string. SAML Assertion for example reuses http://www.w3.org/2000/09/xmldsig# and http://www.w3.org/2001/04/xmlenc#.

It would be very confusing if every instance documents defined xs:int in different namespaces. The prefix like xs can be specified to whatever by the instance, so you can call it foo:int if you want. But the validator needs to know that eventually it resolves to {http://www.w3.org/2001/XMLSchema}int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜