guerda
guerda

Reputation: 24049

How to specify an attribute namespace in XSD so that JAXB interprets it correctly?

I have a small problem with attribute namespaces within an existing XSD. I have to modify this XSD in order to use all functionality of a third-party software.
My goal is to mix an element with a namespaced attribute, like this:

<graphics type="RECTANGLE" cy:nodeLabel="Label 1" />

The <graphics> element is defined in the default namespace, the attribute cy:nodeLabel in a specific namespace.

Currently, my attribute definition in the XSD looks like this:

<xsd:attribute name="nodeLabel" type="xsd:string" form="qualified" xmlns="http://www.cytoscape.org"/>

The option form="qualified" forces JAXB to annotate the @XmlAttribute with namespace="....", but it takes the default namespace, instead of http://www.cytoscape.org. If I change this manually in the generated Java classes, the XML output is as desired.

I would like to define the attribute namespace within the XSD, so that I can rely on JAXB (resp. xjc) to generate the correct Java classes.
How can I specify an attribute namespace for one attribute in the XSD?

Upvotes: 1

Views: 1946

Answers (2)

guerda
guerda

Reputation: 24049

Thanks to jtahlborn's answer, I found the appropriate solution:

Import separate XSD:

New file cytoscape-additions.xsd

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

  <!-- Cytoscape additions Graphics -->
  <xsd:attributeGroup name="cytoscape-addition-graphics">
    <xsd:attribute name="nodeLabel" type="xsd:string" form="qualified"
    xmlns="http://www.cytoscape.org" />
  </xsd:attributeGroup>
</xsd:schema>

Source: https://stackoverflow.com/a/12111103/32043

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

A single xsd file can only define a single namespace. you need a separate xsd which defines the second namespace, which you would then import into the original xsd (and reference the attribute accordingly).

Upvotes: 3

Related Questions