Skip to content

elomagic/xsd-model

Repository files navigation

xsd-model - Simple object model of the XSD file


Apache License, Version 2.0, January 2004 Maven Central build workflow GitHub issues GitHub tag Maintenance Buymeacoffee

What is this xsd-model ?

This project is a Java library to ease up the reading of XSD files.

  • Supports Java 17 or higher
  • Supports Jakarta XML Binding 4.0

Using the library

Maven

Add following dependency to your project. Replace the value of the attribute version according to the used version in your project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

    ...

    <dependencies>
        <dependency>
            <groupId>de.elomagic</groupId>
            <artifactId>xsd-model</artifactId>
            <version>[3,]</version>
        </dependency>
    </dependencies>
    
    ...
    
</project>

Using the API

Read a XSD file

import de.elomagic.xsdmodel.XsdReader;
import de.elomagic.xsdmodel.elements.XsdSchema;

import java.nio.file.Paths;

class Sample {

  void example() throws Exception {
    System.setProperty(XsdSchemaFactory.XSD_SCHEMA_FACTORY_CLASS, XsdSchemaFactoryMock.class.getName());

    XsdSchema schema = XsdReader.read(Paths.get("root2.xsd"));

    Assertions.assertEquals("Documentation of the schema annotation.", schema.getAnnotation().getDocumentation().getValue());
    Assertions.assertEquals(12, schema.getComplexTypes().size());
  }
  
}

Convert XSD to key map

Very experimental implementation of mapping a XSD to key a map.

import de.elomagic.xsdmodel.XsdReader;
import de.elomagic.xsdmodel.converter.Xsd2KeyValueConverter;

import java.nio.file.Paths;

class Sample {

    void example() throws Exception {
        Xsd2KeyValueConverter<KeyProperties> converter = new Xsd2KeyValueConverter<>()
                .setKeyDelimiter(".")
                .setAttributeDelimiter("#")
                .setAttributeSupport(true)
                .setKeyPropertySupplier(KeyProperties::new);

        Map<String, KeyProperties> map = converter.convert(getClass().getResourceAsStream("/example.xsd"));
        map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(e -> System.out.println(e.getKey() + "=" + e.getValue().getDatatype()));
    }
    
}

Limitations

  • No repetition support
  • No attribution support
  • No recursive element type support
  • Simple restriction support

How to build artefact by myself?

What you need is an installed JDK at least version 17 and Apache Maven. Then clone this project to your local file system and execute mvn clean install in the project folder. After successful finish you find the artefact in the target folder.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Versioning

Versioning follows the semantic of Semantic Versioning 2.0.0

Releasing new version / hotfix (Only for users who have repository permissions)

Releasing new version / hotfix

Execute following steps:

Who do I talk to?

  • Repo owner or admin

License

The xsd-model is distributed under Apache License, Version 2.0