xmln:tns and targetNamespace - xsd

I am seeing some XSD schema documents that declare both a targetNamespace and an xmlns:tns attribute in their top schema element. E.g. the following one taken from here. They also seem to have the same string value. I understand the role of targetNamespace but what does xmlns:tns do on top of that?
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/Product"
xmlns:tns="http://www.example.org/Product"
elementFormDefault="qualified">
...

It lets you refer to the namespace later in the schema. For example, if you declare a named type and then want to also declare an element of that type
<complexType name="someType">
<!-- ... -->
</complexType>
<element name="someElement" type="tns:someType" />
Simply saying type="someType" wouldn't work because that would be referring to the (non-existent) someType in the http://www.w3.org/2001/XMLSchema namespace (the xmlns="..." of the schema file) rather than the one in the http://www.example.org/Product namespace.

Related

How to use SimpleFieldExtension in KML?

I'm creating a geopositioning application and we intent to use KML as our import/export data scructure.
We need to store extra information on field definitions, but I'm having trouble understant how to use KML SimpleFieldExtension (in fact my problem is understant XML Schema and validation).
The Google KML tutorial https://developers.google.com/kml/documentation/extendeddata doesn't teach how to do it.
I understand that SimpleFieldExtension is an abstract element and there is no concrect element in the KML specfication.
<element name="SimpleFieldExtension" abstract="true"/>
So I need to extend it and create my own, rigth?
I would like to do something like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Schema name="mySchemaName" id="mySchemaId">
<SimpleField type="xsd:int" name="myValue">
<displayName>MyValue</displayName>
<mySimpleFieldExtension>
<someExtraInfo>...</someExtraInfo>
<otherExtraInfo>...</otherExtraInfo>
</mySimpleFieldExtension>
</SimpleField>
</Schema>
<!-- Some placemarks with myValue fields -->
</Document>
</kml>
When I was trying to figure this out, I came with the impression that I need to create a .xsd file with my own mySimpleFieldExtension, and some how points the .kml file to it. But I'm not sure if that is the right path.
<element name="mySimpleFieldExtension" substitutionGroup="kml:SimpleFieldExtension"/>
Can some one give me an example? Thank you in advance.
I'm using http://www.kmlvalidator.com/ to check my files.
If you want to create SimpleFieldExtension elements and validate it then you will need to create an XML Schema (.xsd) and refer that file in your KML documents.
Example XML Schema with KML extension:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:ext="http://myextension"
targetNamespace="http://myextension"
elementFormDefault="qualified"
version="2.2.0">
<import namespace="http://www.opengis.net/kml/2.2"
schemaLocation="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd" />
<element name="SimpleMetadata" type="ext:SimpleMetadataType"
substitutionGroup="kml:SimpleFieldExtension"/>
<complexType name="SimpleMetadataType" final="#all">
<sequence>
<element name="description" type="string"/>
<element name="observedProperty">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="type" type="string" use="required"/>
</extension>
</simpleContent>
</complexType>
</element>
<any namespace="##other" processContents="lax" minOccurs="0"
maxOccurs="unbounded"/>
</sequence>
</complexType>
</schema>
Here's KML document:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://myextension"
xsi:schemaLocation="http://myextension ext.xsd
http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">
<Document>
<Schema id="SensorTypesId" name="SensorTypes">
<SimpleField name="model" type="string"/>
<SimpleField name="reason" type="string"/>
<SimpleField name="speed" type="double">
<ext:SimpleMetadata>
<ext:description>this is the true air speed of a given
aircraft in meters per second</ext:description>
<ext:observedProperty type="urn:ogc:def:phenomenon:OGC:speed" />
</ext:SimpleMetadata>
</SimpleField>
</Schema>
...
A proposed example of a SimpleFieldExtension and discussion can be found here.
Note that http://www.kmlvalidator.com/ checks the strict KML specification and doesn't check KML extensions such as Google's KML extensions so you won't be able to validate custom extensions either.
You can validate such a KML document using the XML Validator which is a standalone command-line validator.
You'll need to add the namespace definition in the XML Validator ns.map config file:
http://myextension=${XV_HOME}/schemas/ext.xsd
or absolute path like this:
http://myextension=C:/myPath/ext.xsd
Even though the SimpleFieldExtension is supported by the KML standard, adding a custom SimpleFieldExtension through a custom XML Schema requires more testing to verify it doesn't cause problems to applications using it especially if you plan to share your KML outside your organization. Applications like Google Earth will simply ignore your extensions so only use extensions when you absolutely must.

generating JAXB objects from XML

I have a problem, I have an XSD file which I am trying to validate with an XML:
The XSD starts with:
<xs:schema id="Notes"
targetNamespace="http://mynotes.com/Notes"
elementFormDefault="qualified"
xmlns="http://mynotes.com/Notes"
xmlns:mstns="http://mynotes.com/Notes"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
Then I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<notes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="noNamespaceSchemaLocation" xmlns="http://mynotes.com/Notes">
In that case I can validate, but when I use JAXB to create an objects, the objects don't get populated and all their members are null.
But, If I modify the XML to the following:
<?xml version="1.0" encoding="utf-8"?>
<notes>...
In this case the JAXB objects are created successfully, but the validation fails...
I guess I am missing something with the namespace declaration, your help will be highly appreciated.
Thanks.
You can use the package level #XmlSchema annotation to specify the default namespace qualification for your JAXB model. Below is an example, you will need to adjust the package to be the same as your domain classes.
com/example/package-info.java
#XmlSchema(
namespace = "http://mynotes.com/Notes",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

XML Namespaces and validation

today i've bumped in the following problem. I have the following xml:
<c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
...
</c:docschema>
And it's validating fina against it's schema. But I don't want namespace prefixes in my xml, so i try to write it like this:
<docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
...
</docschema>
And it's giving me a validation error. My XSD schema i'm validating against is compound of two XSD's, here is the headers:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="unqualified"
xmlns="http://www.otr.ru/sufd/document/desc"
targetNamespace="http://www.otr.ru/sufd/document/desc"
xmlns:fieldset="http://www.otr.ru/sufd/document/fieldset"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
<xsd:import namespace="http://www.otr.ru/sufd/document/fieldset" schemaLocation="fieldset.xsd"/>
and
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="unqualified"
targetNamespace="http://www.otr.ru/sufd/document/fieldset"
xmlns="http://www.otr.ru/sufd/document/fieldset">
What's wrong there?
EDIT: The question is now, how to change my XSD's in order to make instance document valid?
Given what you write, I imagine that the problem is the following.
Let's consider that there is an a element under your root element.
This first example below is valid because a is unqualified and because you set elementFormDefault to unqualified :
First example
<c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
<a>...</a>
</c:docschema>
In the second example the file is not valid because you set elementFormDefault to unqualified and you have an element a that is qualified (in the default namespace) :
Second example
<docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
<a>...</a>
</docschema>
The correct XML could be :
<docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
<a xmlns="">...</a>
</docschema>
EDIT
If the children of the root element are defined in the same namespace than the root in your schemas, you just have to change elementFormDefault="unqualified" to elementFormDefault="qualified" to have a schema that validates the XML. If it's not the case : you will surely have to reshape your schema more deeply, in this case, maybe you should post another question dedicated to that with more code (including more part of the schemas and instances).
Looks like you are making the mistake of assuming that the nested elements inside your root <docschema> will inherit the namespace defined on that root. They will not.
If you want to get rid of namespace prefixes you will then have to explicitly declare the namespace at every sub-node in your instance document.
Eg
<Root xmlns="http://www.myns.com">
<MyElement1 xmlns="http://www.myns.com">
... etc
</MyElement1>
</Root>
or
<p:Root xmlns:p="http://www.myns.com">
<p:MyElement1>
... etc
</p:MyElement1>
</p:Root>
Which is nicer? I think the second option.

Error when trying to generate schema using XJC

So I parsed a bunch of XMLs to generate a giant schema file using trang which went fine. However, when I try to generate sources using xjc, I get the following error,
xjc reutersXMLSchema.xsd
parsing a schema...
[ERROR] no-xsi: The {target namespace} of an attribute declaration must not match 'http://www.w3.org/2001/XMLSchema-instance'.
line 11 of file:/Users/cqin/Downloads/trang-20081028/xsi.xsd
Failed to parse a schema.
The schema looks like the following,
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" xmlns:filter="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/filter" xmlns:ns0="http://www.reuters.com/ns/2006/05/01/webservices/rkd/News_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/headlineml" xmlns:cache_1="http://www.reuters.com/ns/2008/03/01/webservices/rkd/Cache_1" xmlns:ns2="http://www.reuters.com/ns/2006/05/01/webservices/rkd/TokenManagement_1" xmlns:global="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Common_1" xmlns:a="http://www.w3.org/2005/08/addressing">
<xs:import namespace="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/filter" schemaLocation="filter.xsd"/>
<xs:import namespace="http://schemas.reuters.com/ns/2006/04/14/rmds/webservices/news/headlineml" schemaLocation="ns1.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/Common_1" schemaLocation="global.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/News_1" schemaLocation="reutersXMLSchema.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2006/05/01/webservices/rkd/TokenManagement_1" schemaLocation="ns2.xsd"/>
<xs:import namespace="http://www.reuters.com/ns/2008/03/01/webservices/rkd/Cache_1" schemaLocation="cache_1.xsd"/>
<xs:import namespace="http://www.w3.org/2003/05/soap-envelope" schemaLocation="s.xsd"/>
<xs:import namespace="http://www.w3.org/2005/08/addressing" schemaLocation="a.xsd"/>
<xs:attribute name="type" type="xs:NCName"/>
</xs:schema>
Any idea why it won't work?
I've tried changing the targetNameSpace to something unique but I get more errors so I'm wondering if there is something I can do with the original error.
Thanks!
Yes, this isn't right:
targetNamespace="http://www.w3.org/2001/XMLSchema-instance"
This is saying that the target namespace of your schema is actually the namespace of the Schema definition schema itself (i.e. the schema which defines the things like <xs:schema> and <xs:import> actually mean). This schema is fixed, you can't write your own schema targeting it, which is why XJC is rejecting it.
I've tried changing the targetNameSpace to something unique but I get more errors
Keep trying that, you'll get further than with what you have now. However, the fact that trang is generating this bad schema at all is suggestive that you've done something wrong further back along the line. Perhaps you asked it to generate a combined schema which included the http://www.w3.org/2001/XMLSchema-instance schema itself, which is almost certainly not what you want to do.

XSD annotation and documentation elements, and how to use them

We are creating xml files that we want to be compliant with the following xsd: http://www.topografix.com/gpx/1/1/gpx.xsd This xsd supports '...extending by adding your own elements here ...', see the extensionsType, which I have copied below for convenience.
1) I don't understand whether annotation and documentation are literal element names that would appear in compliant xml. I believe they are not but need confirmation. I'm assuming then that a compliant document would simply have any number of our own custom elements anywhere inside of any [extensions] element, correct?
2) Why are there two pairs of annotation/documentation elements below, with one in a sequence?
<xsd:complexType name="extensionsType">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:any>
</xsd:sequence>
</xsd:complexType>
1) From the XML Schema specification: "Annotations provide for human- and machine-targeted annotations of schema components." Schema authors use xsd:documentation as, say Java or .NET, developers use comments.
Annotations are XML Schema artifacts; they are not to show up in an XML document. And yes, your extensions elements should go under <extensions/>; you may use any namespace, other than http://www.topografix.com/GPX/1/1
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" creator="creator1" xmlns="http://www.topografix.com/GPX/1/1">
<extensions>
<my:element xmlns:my="urn:tempuri-org:some">Hello!</my:element>
</extensions>
</gpx>
2) Hard to say why there are two with the same comment; the difference though is that one documents the complex type, while the other the xsd:any element. I would personally have used different comments, first to explain what the complex type is for, the second just as shown.

Resources