aspose file tools*
The moose likes XML and Related Technologies and the fly likes Discrepancies between an XML file and the XSD it was generated from (via JAXB) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Discrepancies between an XML file and the XSD it was generated from (via JAXB)" Watch "Discrepancies between an XML file and the XSD it was generated from (via JAXB)" New topic
Author

Discrepancies between an XML file and the XSD it was generated from (via JAXB)

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi,

background: My fullest exposure to XML is config files and as a very high-level understanding of WS-*. I now have a vendor that I need to communicate with, via XML. They have XSD's and sample XML files.

So I've got the xml schema file and a sample xml file from the vendor. (I don't know for certain that the one was used to generate the other).

I've used JAXB to generate Java classes from the xsd, and wired up some dummy code and generated an XML file with test data. My XML file does not match the sample the vendor has supplied.

This is what appears at the top of the vendor's sample XML File:


and this is what I get:


1) I've got a "standalone=yes' attribute - does that hurt?

2) my sample has no namespace prefixes (is that the right term?) anywhere. I believe this is alright, but is anyone aware of what JAXB API setting allows me to specify a NS to use in the output XML?

3) I'm totally missing the xmlns:xsi attribute from my root element. I believe that's why I'm getting the output of an xmlns:xsi attribute on my null elements. First, am I correct in that? and two: how do I put the xmlns:xsi element in my root element? I'm assuming it's another JAXB API setting. Or, perhaps this is all driven from the XSD, and the vendor has simply supplied me an XSD they created, and a hand-bombed XML file.

4) what is the purpose of the xmlns:xsi attribute in the XML File?

oh, I can share some code if that would help, but it's almost all generated via JAXB from the XSD. My only code is fairly typical "create some objects, fill them with data, marshal them to XML"

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

All your element names are in the same namespace as the corresponding element names in the sample XML, namely the http://www.foobar.ca/foo namespace. The sample XML uses a namespace prefix and your XML uses the default namespace to cause that to happen, but the two documents should be treated identically by namespace-compliant software.

(Caveat: namespaces are not well understood and there's a lot of software which isn't namespace-compliant.)

In the sample XML, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declares a namespace which is not used anywhere in the document. So it would make no difference if it were removed.

In your XML, you have a couple of "nil" attributes which are in the http://www.w3.org/2001/XMLSchema-instance namespace. Their namespace declarations appear in the element which encloses them rather than in the root element, but that makes no difference to the structure of the document. Namespaces can be declared at any level of the tree and they are inherited at lower levels unless they are overridden. Whether the presence of those attributes is a problem depends on the software that will be processing the document.

I would suggest you do some reading to get up to speed on namespaces. If you're going to be dealing with this sort of thing and you can't recognize a namespace declaration then you can spend a lot of time doing unnecessary things, or miss out necessary things.

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Paul Clapham wrote:All your element names are in the same namespace as the corresponding element names in the sample XML, namely the http://www.foobar.ca/foo namespace. The sample XML uses a namespace prefix and your XML uses the default namespace to cause that to happen, but the two documents should be treated identically by namespace-compliant software.

Since posting, I've done further reading (multiple times) in these documents:
http://www.w3.org/TR/xmlschema-0/
http://www.w3.org/TR/1999/REC-xml-names-19990114/

So I now have a basic understanding of the above.

Paul Clapham wrote:(Caveat: namespaces are not well understood and there's a lot of software which isn't namespace-compliant.)

This is what I'm actually concerned about.

Paul Clapham wrote:In the sample XML, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declares a namespace which is not used anywhere in the document. So it would make no difference if it were removed.

In your XML, you have a couple of "nil" attributes which are in the http://www.w3.org/2001/XMLSchema-instance namespace. Their namespace declarations appear in the element which encloses them rather than in the root element, but that makes no difference to the structure of the document. Namespaces can be declared at any level of the tree and they are inherited at lower levels unless they are overridden. Whether the presence of those attributes is a problem depends on the software that will be processing the document.

I understand in the sample that namespace is not used, but in my generated xml doc, it is (or rather, could have been). And I get now that it's a scoping thing, that you either "map" the namespace in the root element, and it is inherited throughout the document. Or you can do it individually for each element that has an attribute (or that contains a sub-element) from a different namespace (like is done in my generated xml).

My concern is that I want my generated document to be as close as possible to the sample provided by the vendor. And in the sample, they've got this:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" right at the top. This would allow me to use just xsi:nil="true" in each element below, instead of having to repeat the xmlns:xsi everywhere.

It seems to me, there should be a way to generate the xml in that format. It would certainly result in smaller output files. If not, I suppose I could post-process it. (XSLT?)


Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Ahh... that is satisfying.

I saw on this page: https://jaxb.dev.java.net/guide/Changing_prefixes.html there is a way to change the prefix that is output (in my case, it uses the default namespace, and I wanted it to use a prefix).

Downloading the distribution (finally having found it on java.net - how frustrating), and viewing the namespace-prefix sample, I see how to put my own prefix on all elements.

The other issue I was having was that xmlns:xsi was being declared on each element that had use of xsi:nill. Having found this:
https://jaxb-architecture-document.dev.java.net/nonav/doc/index.html?com/sun/xml/bind/marshaller/NamespacePrefixMapper.html

I see that the solution of both of my outstanding problems are resolved by implementing this class.

now I only need to figure out what standalone=yes means.

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

For completeness, my class looks like:


and my marshal code has a few additional lines:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Discrepancies between an XML file and the XSD it was generated from (via JAXB)