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
Related
I want to export this in xml using jaxb but i am getting only last record two times using arraylist java.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ROWS><row> <name>john</name><id>1</id><city>xyz</city></row> <row> <name>monu</name><id>2</id><city>abc</city></row> </rows>
Using JAXB, I tried unmarshalling (xml to object)and marshalling (object to xml). I have noticed that there is slight difference in xml formatting, though I feel technically both are same.
Original xml looks as follows:
<?xml version="1.0"?>
<Sites>
<Site id="101" name="NY-01" location="New York" xmlns="http://example.ipsoft.com/coding.xsd">
<Hosts>
<Host id="1001">
<Host_Name>srv001001</Host_Name>
<IP_address>10.1.2.3</IP_address>
<OS>Windows</OS>
<Load_avg_1min>1.3</Load_avg_1min>
<Load_avg_5min>2.5</Load_avg_5min>
<Load_avg_15min>1.2</Load_avg_15min>
</Host>
</Hosts>
</Site>
</Sites>
After marshalling the object to xml, the output looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Sites xmlns:ns2="http://example.ipsoft.com/coding.xsd">
<ns2:Site location="New York" name="NY-01" id="101">
<ns2:Hosts>
<ns2:Host id="1001">
<ns2:Host_Name>srv001001</ns2:Host_Name>
<ns2:IP_address>10.1.2.3</ns2:IP_address>
<ns2:OS>Windows</ns2:OS>
<ns2:Load_avg_1min>1.3</ns2:Load_avg_1min>
<ns2:Load_avg_5min>2.5</ns2:Load_avg_5min>
<ns2:Load_avg_15min>1.2</ns2:Load_avg_15min>
</ns2:Host>
</Sites>
How do i make both the input source xml and final output the same?
The document XML documents have equivalent namespace qualification.
When JAXB Declares a Default Namepsace
When you specify the following package level #XmlSchema annotation then a JAXB implementation will typically leverage a default namespace:
#XmlSchema(
namespace = "http://example.ipsoft.com/coding.xsd"",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Why JAXB is not Using a Default Namespace Here
In your XML document not all of your XML elements are namespace qualified (Site is, but Sites is not) so JAXB saves no prefix to mean no namespace instead of the default namespace.
<Sites>
<Site id="101" name="NY-01" location="New York" xmlns="http://example.ipsoft.com/coding.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.
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.
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.