1

Currently i have to hard-code the schema version (or parse it) manually in java code when working with generated JAXB classes. This can easily lead to mistakes when changing the XML schema version and feels wrong. What i want is to specify the schema version in the schema and let xjc generate a constant in the corresponding root element class.

I haven't found a JAXB plugin or binding mechanism which i can use to meet these requirements.

example.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:my="my:namespace:v1"
    targetNamespace="my:namespace:v1"
    xmlns="my:namespace:v1"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"

    <!-- Use this version information in generated class! -->
    version="1.0">

    <xs:element name="root" type="RootType"/>

    <xs:complexType name="RootType">
        <xs:sequence>
            <xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
        <xs:attribute name="xsdVersion" type="xs:string" use="required"/>
        [...]
</xs:schema>

Generated RootType.class:

[...]
public class RootType {
    protected String name;
    protected String xsdVersion;
    // This shall be generated too
    public static final GENERATED_WITH_VERSION = "1.0";
    [...]
}
Lesurglesum
  • 515
  • 4
  • 10
  • You should read [this](https://stackoverflow.com/questions/2014237/what-are-the-best-practices-for-versioning-xml-schemas) about XML schema versioning, or my [answer](https://stackoverflow.com/questions/52109685/jaxb-how-to-replace-a-class-binding-in-given-object-tree-while-unmarshalling/52119270#52119270) to a similar question. – m4gic Sep 13 '18 at 08:32
  • @m4gic Thanks for your comment! We already have a conversion/upgrade mechanism (XSLT) for old XML-files and don't need (and don't want) to have "old" JAXB classes in current versions of the programme. There's a requirement to not load XML files which have been "built" with a newer XSD version (major or minor) than the XSD used by the programme (prevents possible data loss). So the programme needs to know the XSD (and/or) JAXB class version (major and minor) at runtime. I don't see which information you have linked does help in this situation. – Lesurglesum Sep 15 '18 at 21:44
  • I would have liked to pay your attention that there is (maybe better) alternatives. What you could definitely do in your situation, is to specify a default value for your element in the XSD. – m4gic Sep 16 '18 at 07:22
  • I wanted to specify a default value too but an attribute cannot be set to required when a default value is given which leads to new problems. Ok, thank you! Unfortunately none of the alternatives meet the requirements. Mabye i have to write a JAXB plugin to get the desired result. – Lesurglesum Sep 16 '18 at 18:31
  • Last [link](https://stackoverflow.com/questions/12040826/jaxb-objects-initialized-with-default-values), I promise! :) It seems there is a plugin for that. – m4gic Sep 16 '18 at 20:30
  • Thank you for your efforts! :) But I cannot use a default attribute because the field wouldn't be required anymore. If one creates a XML file and doesn't set xsdVersion attribute I won't know the version. I've done some research on JAXB plugins and XJC and I wonder that i cannot use the attribute (which is part of the official schema component specification) easily. Apparently there is no way to get the schema version when XJC has generated the model and the plugin has to parse the schemata again. – Lesurglesum Sep 17 '18 at 01:37

0 Answers0