Ryan Delucchi
Ryan Delucchi

Reputation: 7748

How do I write a Schema that suppresses elements with a particular attribute value?

I am relatively new to JAXB, but I have a question that seems to be difficult to resolve via online documentation. My goal is simple: I have XML that is being parsed using JAXB, such that elements where included=false should not be part of the resulting object model.

I would like to solve this within the schema:

<complexType name="baseFilterType">
    <simpleContent>
        <extension base="string">
            <attribute type="string" name="code" use="required"/>
            <attribute type="boolean" name="isMandatory" default="false"/>
            <attribute type="boolean" name="included" default="true"/>
        </extension>
    </simpleContent>
</complexType>

<complexType name="baseFiltersType">
    <sequence>
        <element type="rptt:baseFilterType" name="filter" maxOccurs="unbounded" 
          minOccurs="0" />
    </sequence>
</complexType>

So, how do I change the schema shown above such that the any baseFilterType where included = false will not be apart of the object model generated for a baseFiltersType object.

Upvotes: 2

Views: 69

Answers (1)

bdoughan
bdoughan

Reputation: 149047

You could leverage the @XmlPath extension in EclipseLink JAXB (MOXy), I'm the MOXy tech lead.

@XmlRootElement
public class BaseFiltersType {

    private List<BaseFilterType> filter;

    @XmlPath("filter[@included='true']")
    public List<BaseFilterType> getFilter() {
        return filter;
    }

    public void setFilter(List<BaseFilterType> baseFilterType) {
        this.filter = baseFilterType;
    }

}

Answer to Related Question

In one of my answers to a related question I demonstrate how to use MOXy's @XmlPath annotation, and a StAX StreamFilter approach that would work with any JAXB implementation:

For More Information:

Upvotes: 2

Related Questions