• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For completeness, my class looks like:


and my marshal code has a few additional lines:
 
reply
    Bookmark Topic Watch Topic
  • New Topic