0

I have xsd schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="urn:v1"
  xmlns="urn:v1"
  xmlns:reg="urn:v1"
  xmlns:xop="http://www.w3.org/2004/08/xop/include"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
  version="1.1.1">

  <xs:simpleType name="ItemType">    
    <xs:restriction base="xs:string">

    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="ListItemType">   
    <xs:list itemType="ItemType"/>
  </xs:simpleType>

  <xs:complexType name="Element">    
    <xs:sequence>
      <xs:element name="SubElement">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="SubSubElement" type="ListItemType" nillable="true" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>  
</xs:schema>

and java class generated by JIBX:

public class Element
{
    private java.util.List<String> subElements = new java.util.ArrayList<String>();

    /** 
     * Get the list of 'SubSubElement' element items.
     * 
     * @return list
     */
    public java.util.List<String> getSubElements() {
        return subElements;
    }

    /** 
     * Set the list of 'SubSubElement' element items.
     * 
     * @param list
     */
    public void setSubElements(java.util.List<String> list) {
        subElements = list;
    }

    /** 
     * Serializer for 'SubSubElement' element list.
     * 
     * @param values
     * @return text
     */
    public static String serializeSubElements(java.util.List<String> values) {
        if (values == null) {
            return null;
        } else {
            java.lang.StringBuffer buff = new java.lang.StringBuffer();
            for (java.util.Iterator<String> iter = values.iterator(); iter
                    .hasNext();) {
                if (buff.length() > 0) {
                    buff.append(' ');
                }
                String value = iter.next();
                buff.append(value);
            }
            return buff.toString();
        }
    }

    /** 
     * Deserializer for 'SubSubElement' element list.
     * 
     * @param text
     * @return values
     * @throws org.jibx.runtime.JiBXException on conversion error
     */
    public static java.util.List<String> deserializeSubElements(String text)
            throws org.jibx.runtime.JiBXException {
        org.jibx.runtime.IListItemDeserializer ldser = new org.jibx.runtime.IListItemDeserializer() {
            public java.lang.Object deserialize(String text) {
                return text;
            }
        };
        return (java.util.List<String>) org.jibx.runtime.Utility
                .deserializeList(text, ldser);
    }
}

how I can check if element is nillable ? which I have in xsd: nillable="true"

UPDATE:

ok some esier xsd without list:

<xs:schema targetNamespace="urn:v1" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.1.1" xmlns="urn:v1" xmlns:reg="urn:v1" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="Element">
         <xs:sequence minOccurs="0">
            <xs:element name="SubElement" type="type1" nillable="true" minOccurs="0"/>
         </xs:sequence>
   </xs:complexType>

    <xs:simpleType name="type1">        
        <xs:restriction base="xs:string" />
    </xs:simpleType>

</xs:schema>


public class Element
{
    private String subElement;

    /** 
     * Get the 'SubElement' element value.
     * 
     * @return value
     */
    public String getSubElement() {
        return subElement;
    }

    /** 
     * Set the 'SubElement' element value.
     * 
     * @param subElement
     */
    public void setSubElement(String subElement) {
        this.subElement = subElement;
    }
}

again no nillable

thobens
  • 1,729
  • 1
  • 15
  • 34
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Why do you need such check? What is the goal? Do you mean check for presence of `nillable="true"` in schema or for presence of `xsi:nil="true"` in real data? – Vadzim Sep 26 '12 at 13:52
  • in JAXB I can do this: if (elementType.getElement().isNil()){ do something } and now I need to do same thing in JIBX if element is nill (nillable=true) do something – hudi Sep 26 '12 at 14:01

1 Answers1

0

You have here xs:element name="SubSubElement" type="ListItemType" nillable="true" minOccurs="0" that translates to java.util.List<String> getSubElements().

Every null value in resulting list would correspond to <SubSubElement xsi:nil="true"/> in xml data.

There is no other practical way for nulls to appear in the list.

So it seems to be enough just to check list items for null in this case.

See http://www.ibm.com/developerworks/xml/library/ws-tip-null/index.html#N10142.

Update

JiBX docs state

The xsi:nil attribute, also used in instance documents, was not supported by JiBX 1.0. JiBX 1.1 added support for this feature using the nillable attribute in the object attribute group.

and then

This attribute can only be used with objects that are bound to an element name.

So the desired check is definitely not possible at all with mapping to java.util.List<String> getSubElements().

You could change mapping to java.util.List<SubElement> getSubElements(). <SubSubElement/> would map to empty SubElement instance then.

Quick full-text search through all JiBX sources didn't show up any notion for nil check possibility. It seems JiBX support for nillable is limited only to special marshalling rules. It makes sense assuming nillable is supported only for element-bound mappings.

Example xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="urn:v1"
  xmlns="urn:v1"
  xmlns:reg="urn:v1"
  xmlns:xop="http://www.w3.org/2004/08/xop/include"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
  version="1.1.1">

  <xs:complexType name="SubElement">    
    <xs:simpleContent>
      <xs:extension base="xs:string">
      </xs:extension>
    </xs:simpleContent>    
  </xs:complexType>
  <xs:simpleType name="SubElementList">   
    <xs:list itemType="SubElement"/>
  </xs:simpleType>
  <xs:complexType name="Element">    
    <xs:sequence>
      <xs:element name="SubElement">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="SubSubElement" type="SubElementList" nillable="true" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>  
</xs:schema>
Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • and can you explain me how I can distinguish between and ? in both of this xml is list null. JAXB support to get if element is nillable and now I need to find out how is JIBX implement this functionality – hudi Sep 26 '12 at 17:12
  • Maybe I'm missing something, but what is the practical reason to difference `` and ``? (Handling empty strings maybe?) Would not both map to null at the client side anyway? – Vadzim Sep 27 '12 at 06:54
  • In my application when client use nillable in element I want to delete something. When it send just empty I do nothing. – hudi Sep 27 '12 at 07:19
  • I update question to easier xsd without list but still no nillable check. Can you provided some example with nillable check ? Because as you write in JIBX 1.1 is nillable supported but I dont know how – hudi Sep 27 '12 at 07:37
  • I suggested mapping `SubSubElement` to `SubSubElement` class with string field. Then `` would map to null while `` would map to not null `SubSubElement` instance with null string field. – Vadzim Sep 27 '12 at 07:45
  • Can you explain my your first sentence. I dont understand what you mean with: I suggested mapping SubSubElement to SubSubElement class with string field. maybe with some example – hudi Sep 27 '12 at 08:00
  • I've updated the answer with example xsd that forced JiBX to generate `SubElement` class. – Vadzim Oct 01 '12 at 12:39