xsdjaxbschemagenannotated-pojos

Where does xmlns:tns come from when using schemagen


We are using schemagen to create an XSD from some annotated POJOs.

Here is our ant target

<target name="generate-xsd" depends="compile">
    <taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask" 
                     classpathref="xjc.classpath"/>
    <schemagen srcdir="src" destdir="generated" includeantruntime="false">
        <include name="com/acme/exam/delivery/records/**"/>
        <schema namespace="http://www.acme.com/deliverylog" 
                            file="deliverylog.xsd"/>
        <schema namespace="" file="supplemental.xsd"/> 
    </schemagen>
</target>

This is generating

<xs:schema elementFormDefault="qualified" version="1.0" 
           targetNamespace="http://www.acme.com/deliverylog" 
           xmlns:tns="http://www.acme.com/deliverylog" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

Where does the tns namespace come from and what does it signify?


Solution

  • That infomration comes from the package level annotation @XmlSchema which can be found in the package-info class. See below for an example.

    package-info

    @XmlSchema(
        namespace = "http://www.acme.com/deliverylo",
        elementFormDefault = XmlNsForm.QUALIFIED)
    package example;
    
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    

    Sample XML

    elementFormDefault specifies which elements should be namespace qualified (true = all, false = only global elements), and targetNamespace defines what the namespace is.

    <foo xmlns="http://www.acme.com/deliverylog">
        <bar>Hello World</bar>
    </foo>
    

    For More Information