Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some people may say: if you can edit both XSDs, why bother with redefine? </p> <p>I'll show you how you can make it work with XSD redefine, at least from an XSD perspective. However, given limitations with JAXB, it’ll not work with it out of the box. If you also use automatic XSD refactoring, as an extra step, then you can make it work and, in the process, you'll be preserving the value proposition that you see when using xsd:redefine.</p> <p>So, before that, here is another way which also uses composition, but without <code>xsd:redefine</code>; from a maintenance and validation perspective, you're getting about the same value and usage.</p> <p>I'll refer to your first XSD as <code>Model1</code> and your second XSD as <code>Model2</code>. I'll start with one XSD that will give you the "reuse-through-composition" aspect you have with <code>xs:redefine</code>.</p> <p>Common items, <strong>xsd-allow-extension-compatibility-and-validation-common-items.xsd</strong>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)--&gt; &lt;xs:schema xmlns="example" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="example" elementFormDefault="qualified" attributeFormDefault="qualified"&gt; &lt;xs:group name="typeA"&gt; &lt;xs:sequence&gt; &lt;xs:element name="elA" type="xs:string" /&gt; &lt;/xs:sequence&gt; &lt;/xs:group&gt; &lt;xs:element name="root" type="typeA" /&gt; &lt;/xs:schema&gt; </code></pre> <p>Model1 "items", <strong>xsd-allow-extension-compatibility-and-validation-model1-items.xsd</strong>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)--&gt; &lt;xs:schema xmlns="example" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="example" elementFormDefault="qualified" attributeFormDefault="qualified"&gt; &lt;xs:complexType name="typeA"&gt; &lt;xs:sequence&gt; &lt;xs:group ref="typeA" /&gt; &lt;xs:any namespace="##any" minOccurs="0" processContents="lax" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>Model2 "items", <strong>xsd-allow-extension-compatibility-and-validation-model2-items.xsd</strong>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)--&gt; &lt;xs:schema xmlns="example" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="example" elementFormDefault="qualified" attributeFormDefault="qualified"&gt; &lt;xs:complexType name="typeA"&gt; &lt;xs:sequence&gt; &lt;xs:group ref="typeA" /&gt; &lt;xs:element name="newElement" type="xs:string" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>If you pass Common Items and Model1, or Common Items and Model2 to JAXB compiler, it'll create the classes exactly the way you want. For easy of use (testing) and illustration, I've created two more XSDs:</p> <p>Model1:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)--&gt; &lt;xs:schema xmlns="example" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="example" elementFormDefault="qualified" attributeFormDefault="qualified"&gt; &lt;xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-common-items.xsd"/&gt; &lt;xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-model1-items.xsd"/&gt; &lt;/xs:schema&gt; </code></pre> <p>Model2:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)--&gt; &lt;xs:schema xmlns="example" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-common-items.xsd"/&gt; &lt;xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-model2-items.xsd"/&gt; &lt;/xs:schema&gt; </code></pre> <p>This is what you get when you run xjc agains Model1:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "typeA", propOrder = { "elA", "any" }) public class TypeA { @XmlElement(required = true) protected String elA; @XmlAnyElement(lax = true) protected Object any; /** * Gets the value of the elA property. * * @return * possible object is * {@link String } * */ public String getElA() { return elA; } /** * Sets the value of the elA property. * * @param value * allowed object is * {@link String } * */ public void setElA(String value) { this.elA = value; } /** * Gets the value of the any property. * * @return * possible object is * {@link Element } * {@link Object } * */ public Object getAny() { return any; } /** * Sets the value of the any property. * * @param value * allowed object is * {@link Element } * {@link Object } * */ public void setAny(Object value) { this.any = value; } } </code></pre> <p>... and when you run xjc agains Model2:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "typeA", propOrder = { "elA", "newElement" }) public class TypeA { @XmlElement(required = true) protected String elA; @XmlElement(required = true) protected String newElement; /** * Gets the value of the elA property. * * @return * possible object is * {@link String } * */ public String getElA() { return elA; } /** * Sets the value of the elA property. * * @param value * allowed object is * {@link String } * */ public void setElA(String value) { this.elA = value; } /** * Gets the value of the newElement property. * * @return * possible object is * {@link String } * */ public String getNewElement() { return newElement; } /** * Sets the value of the newElement property. * * @param value * allowed object is * {@link String } * */ public void setNewElement(String value) { this.newElement = value; } } </code></pre> <p>Model1 and Model2 XSDs will validate your XMLs exactly the way you've intended. </p> <p>Below if a diagram showing the relationship between the XSD files. Green means "xsd:include", and the arrow points to the "included".</p> <p><img src="https://i.stack.imgur.com/eDQ3T.jpg" alt="QTAssistant XSD file diagram"></p> <p>UPDATE: I just noticed, based on @Kevin 's comment, that you don't have a maxOccurs on your new element in the redefine. In this case, you could use one single redefine, like so:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)--&gt; &lt;xsd:schema xmlns="example" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;xsd:redefine schemaLocation="xsd-allow-extension-compatibility-and-validation.xsd"&gt; &lt;xsd:complexType name="typeA"&gt; &lt;xsd:complexContent&gt; &lt;xsd:restriction base="typeA"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="elA" type="xsd:string" /&gt; &lt;xsd:element name="newElement" type="xsd:string" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:restriction&gt; &lt;/xsd:complexContent&gt; &lt;/xsd:complexType&gt; &lt;/xsd:redefine&gt; &lt;/xsd:schema&gt; </code></pre> <p>The only problem seems to be that JAXB (latest) still generates a class using the wildcard.</p> <p>Update 2: Based on Kevin's comment, two avoid two redefines, a group should be used instead of xsd:any.</p> <p>If you in fact plan to use more than one element to extend the new model, then read on. Below is the only way to do that, which requires the use of a group to further refine the any particles.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)--&gt; &lt;xsd:schema xmlns="example" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;xsd:redefine schemaLocation="xsd-allow-extension-compatibility-and-validation.xsd"&gt; &lt;xsd:complexType name="typeA"&gt; &lt;xsd:complexContent&gt; &lt;xsd:restriction base="typeA"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="elA" type="xsd:string" /&gt; &lt;xsd:group ref="group1" minOccurs="0"&gt; &lt;/xsd:sequence&gt; &lt;/xsd:restriction&gt; &lt;/xsd:complexContent&gt; &lt;/xsd:complexType&gt; &lt;/xsd:redefine&gt; &lt;xsd:group name="group1"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="newElement" type="xsd:string" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:group&gt; &lt;/xsd:schema&gt; </code></pre> <p>The net result is that the new XSD can be used to validate Model1, while the original file remains Model1. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload