XML Namespaces and validation - xsd

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.

Related

xmln:tns and targetNamespace

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.

Intellij jaxb xjc schema location for content assist

I'm using JAXB for generating code. Now I'm trying to add the XJC namespace to the custom bindings.xjb file, but IntelliJ Idea 12 won't find the namespace. The xmlns:xjc="..." row is marked red, and when I'm trying to use the "Fetch external resource" feature, it tells me No XML at the location: http://java.sun.com/xml/ns/jaxb/xjc. Where can I find the xsd file for the xjc namespace to activate content assist?
Heres the definition in my .xjb file:
<jxb:bindings version="2.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
JAXB distribution has this file in
lib\jaxb-xjc.src.zip\com\sun\tools\xjc\reader\xmlschema\bindinfo\xjc.‌​xsd
unpack it and configure a namespace mapping in Settings | Schemas and DTDs.

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

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.

How can I tell wsimport that separate WSDL files are referring to the same object classes?

I have three different JAX-WS services which use the same classes on the server (e.g. ServiceA, ServiceB, and ServiceC, all of which use MyCommonClass as a parameter). Another module we are developing is using wsimport to create a client for these services, however the problem is that wsimport creates separate instances of MyCommonClass for each service:
com.company.servicea.endpoint.MyCommonClass
com.company.serviceb.endpoint.MyCommonClass
etc.
I know that I could use the wsimport -p option to specify a common package for each endpoint, however I'd like to keep most of the classes in separate packages, but just to share certain common ones. From what I have read it sounds like a JAXB bindings file(s) might be able to help, but I haven't yet figured out the exact syntax to achieve the desired result. I think I'll need a separate bindings file for each service (as I call wsimport once for each one), which looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1" xmlns:tns="http://endpoint.servicea.company.com/">
<bindings node="//xsd:complexType[#name='myCommonClass']">
<class name="com.company.model.MyCommonClass"/>
</bindings>
</bindings>
Am I on the right track? Or do you have any alternative solutions to the problem?
Define your common classes in an xsd and import it in to the service WSDL's.
Then use the schema customization to generate definitions in this schema in to a separate package like "com.company.model"
<jxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="1.0">
<jxb:bindings schemaLocation="model.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.company.model"/>
</jxb:schemaBindings>
</jxb:bindings>
...
ref: http://jax-ws.java.net/jax-ws-21-ea1/docs/customizations.html#2.6_Class_Customization

Resources