1

I have a question regarding the node xpath expression in <jxb:bindings\>:

I have a type that I use multiple times in an xsd, specified in its own xsd:

common.xsd:

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                                                                                                                           
<xsd:schema                                                                                                                 
  targetNamespace="http://www.example.com/xsd/common"                                                                       
  xmlns="http://www.example.com/xsd/common"                                                                                 
  xmlns:tns="http://www.example.com/xsd/common"                                                                             
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"                                                                              
  elementFormDefault="qualified">                                                                                           

  <xsd:complexType name="Parameter">
   <xsd:simpleContent>
     <xsd:extension base="xsd:string">
        <xsd:attribute name="key" type="xsd:string" use="required" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>
</xsd:schema>

included multiple times in another schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
  targetNamespace="http://www.example.com/xsd/example"
  xmlns="http://www.example.com/xsd/example"
  xmlns:tns="http://www.example.com/xsd/example"
  xmlns:c="http://www.example.com/xsd/common"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">

<xsd:element name="example" type="example" />

<xsd:complexType name="example">
  <xsd:all>
    <xsd:element name="working" type="working" />
    <xsd:element name="notworking" type="notworking" />
  </xsd:all>
</xsd:complexType>

<xsd:complexType name="working">
  <xsd:all>
    <xsd:element name="Parameters">
      <xsd:sequence>
        <xsd:element name="Parameter" type="c:Parameter" maxOccurs="unbounded" />
      </xsd:sequence>
    </xsd:element>
  </xsd:all>
</xsd:complexType>

<xsd:complexType name="notworking">
  <xsd:sequence>
    <xsd:element name="Parameter" type="c:Parameter" maxOccurs="unbounded" />
  </xsd:sequence>
  <xsd:attribute name="anAttribute" type="xsd:token" />
</xsd:complexType>

</xsd:schema>

for binding with jaxb, I have a class ParameterMap:

package com.example.common.xml.model;

import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.example.common.xml.binding.ParameterAdapter;

@XmlJavaTypeAdapter(ParameterAdapter.class)
public class ParameterMap extends HashMap<String, String> {}

as well as the adapter class:

package com.example.common.xml.binding;

import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.example.common.xml.model.ObjectFactory;
import com.example.common.xml.model.Parameter;
import com.example.common.xml.model.ParameterMap;

public class ParameterAdapter extends XmlAdapter<List<Parameter>, ParameterMap> {
  public ParameterMap unmarshal(List<Parameter> params) {
    ParameterMap map = new ParameterMap();
    for (Parameter p : params) {
      map.put(p.getKey(), p.getValue());
    }
    return map;
  }

  public List<Parameter> marshal(ParameterMap map) {
    List<Parameter> params = new ArrayList<>();
    ObjectFactory of = new ObjectFactory();
    for (Map.Entry<String, String> e : map.entrySet()) {
      Parameter p = of.createParameter();
      p.setKey(e.getKey());
      p.setValue(e.getValue());
      params.add(p);
    }
    return params;
  }
}

and of course bindings files to make the classes Parameter and ObjectFactory be generated into the correct package. The bindings file for the example namespace is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="1.0"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jxb:extensionBindingPrefixes="xjc">

  <jxb:bindings schemaLocation="../pathTo/example.xsd" node="/xs:schema">

    <jxb:bindings node="//xs:complexType[@name='working']//xs:element[@name='Parameters']">
      <jxb:class name="com.example.common.xml.model.ParameterMap" />
    </jxb:bindings>

    <jxb:bindings node="//xs:complexType[@name='notworking']//xs:element[@name='Parameter']">
      <jxb:class name="com.example.common.xml.model.ParameterMap" />
    </jxb:bindings>

  </jxb:bindings>

</jxb:bindings>

as you can tell from the naming, the binding for the child of working gets generated correctly, while the binding for notworking results in the generated class member

@XmlElement(name = "Parameter")
protected List<ParameterMap> parameter;

what I would that like to look like would be the same as in working:

@XmlElement(name="Parameters")
protected ParameterMap parameters;

I'm not flashed by "Parameter" vs "Parameters", but the type is wrong - any help on how to achieve that would be appreciated.

I also already tried applying the notworking binding to the sequence (via node="//xs:complexType[@name='notworking']/xs:sequence") but that just gets ignored. If I try to apply it directly on the notworking complexType it results in a parse error.

tjanu
  • 172
  • 2
  • 11

1 Answers1

1

The problem is not in the XPath, it apparently works fine.

The problem is that this approach won't work. jaxb:class will not convert your collection property into a single property, it only configures the type of the collection item.

You could write an XJC plugin for this, but that's somewhat complicated.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks! As I’m not exactly an xpath guru I was hoping for a simple solution, but I guess I’ll have to try my hand at writing an xjc plugin. – tjanu May 04 '18 at 07:29
  • @tjanu [This](https://github.com/highsource/jaxb2-basics/blob/master/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/simplify/SimplifyPlugin.java) might help. See also: https://stackoverflow.com/questions/9247730/what-is-the-role-of-classoutline-jclass-cclass-in-codemodel/9404341#9404341 – lexicore May 04 '18 at 10:59