I need some help with my xsd and jaxb.
My xsd leads to an working class (MyWorkingType) and the 'ProblemType', which is not generated with an enum.
As I believe, that both forms are valid, why does this not work, where is the bug (jaxb or at the keyboard?)
Edit: I add my ant-script, because Xtian could create this via maven-plugin.
This is my xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ProblemType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="workingEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="MyWorkingType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute" type="workingEnum" />
</xs:sequence>
</xs:complexType>
</xs:schema>
This is my ant-script I use within eclipse:
<?xml version="1.0" encoding="UTF-8"?>
<project name="generate" default="generate-jaxb" basedir=".">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath>
<fileset dir="../lib" includes="*.jar" />
</classpath>
</taskdef>
<target name="generate-jaxb">
<xjc destdir="../../src-gen" package="test.gen">
<arg value="-no-header" />
<schema dir="xsd/" includes="*.xsd"/>
</xjc>
</target>
</project>
Try specifying typesafeEnumBase. This is what I normally do when generating mappings:
<jaxb:bindings schemaLocation="mySchema.xsd"
node="/xs:schema">
<jaxb:globalBindings
fixedAttributeAsConstantProperty="false"
typesafeEnumBase="xs:string"
typesafeEnumMemberName="generateName"
generateIsSetMethod="true">
<xjc:noValidator />
<xjc:noValidatingUnmarshaller />
</jaxb:globalBindings>
<jaxb:schemaBindings>
<jaxb:package name="my.package.name"/>
</jaxb:schemaBindings>
</jaxb:bindings>
However I don't think this is the cause.
Since my maven-jaxb2-plugin works and xjc via Ant does not, the question, what is the difference.
Actually, strict, verbose and extension settings should make no difference for enums.
However it may be that maven-jaxb2-plugin uses a newer version of xjc than you're using with enum. Please try to update your xjc and see if it helps.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ProblemType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute">
<xs:simpleType name="problemTypeEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="workingEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="MyWorkingType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute" type="workingEnum" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I generated the class by this maven plugin
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>
<executions>
<execution>
<id>commun-generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generateDirectory>${basedir}/src/main/java/</generateDirectory>
<schemaDirectory>${basedir}/src/main/resources/schema/xsd</schemaDirectory>
<strict>false</strict>
<extension>true</extension>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
ProblemTypeEnum.java
#XmlType(name = "problemTypeEnum")
#XmlEnum
public enum ProblemTypeEnum {
VALUE_1,
VALUE_2,
VALUE_3;
public String value() {
return name();
}
public static ProblemTypeEnum fromValue(String v) {
return valueOf(v);
}
}
If you can change the structure .. this is the xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ProblemType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true"
type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute"
type="problemTypeEnum" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="problemTypeEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="workingEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="VALUE_1" />
<xs:enumeration value="VALUE_2" />
<xs:enumeration value="VALUE_3" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="MyWorkingType">
<xs:sequence>
<xs:element minOccurs="0" name="normalAttribute" nillable="true"
type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="1" name="enumAttribute"
type="workingEnum" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Related
In the MSDN documentation, I see fragments that look like XSD for the PROPDESC files for things like propertyDescriptionList attributes. Is the whole schema definition somewhere so I can validate my .propdesc file?
Here's the sample propdesc file from the Windows 7 SDK sample, for what that's worth. The sample defines a custom file type of .recipe.
<?xml version="1.0" encoding="utf-8"?>
<!--
This propdesc file contains the descriptions of Recipe Sample custom properties.
To register/unregister, use the PropertySchema SDK sample, or http://www.codeplex.com/prop.
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/windows/2006/propertydescription"
schemaVersion="1.0">
<propertyDescriptionList publisher="Microsoft" product="SampleRecipe">
<propertyDescription name="Microsoft.SampleRecipe.Difficulty" formatID="{1794C9FE-74A9-497f-9C69-B31F03CE7EF9}" propID="100">
<description>This property indicates the preparation difficulty of a recipe.</description>
<searchInfo inInvertedIndex="true" isColumn="true" columnIndexType="OnDisk" mnemonics="RecipeDifficulty"/>
<typeInfo type="String" multipleValues="false" isViewable="true" isQueryable="true"/>
<labelInfo label="Recipe difficulty" invitationText="Specify recipe difficulty" />
<displayInfo displayType="Enumerated" >
<editControl control="DropList"/>
<enumeratedList>
<enum value="Easy" text="Easy" />
<enum value="Medium" text="Medium" />
<enum value="Hard" text="Hard" />
</enumeratedList>
</displayInfo>
</propertyDescription>
</propertyDescriptionList>
</schema>
Update: there's a note at MSDN requiring an xmlns reference to http://schemas.microsoft.com/windows/2006/propertydescription but that's just a placeholder URL as far as I can tell.
I finally just created an empty xsd file in Visual Studio and started plugging in the various pieces. I got it working well enough, with the exception of missing simple types for upcase-uuid and canonical-name, which I don't see in the docs. Anyway, I was able to use the XML Plugin in Notepad++ to validate my .propdesc file against this schema, and it found an error for me, so I thought I'd share.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://schemas.microsoft.com/windows/2006/propertydescription"
elementFormDefault="qualified"
xmlns="http://schemas.microsoft.com/windows/2006/propertydescription"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="schema">
<xs:complexType>
<xs:sequence>
<xs:element ref="propertyDescriptionList" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="schemaVersion" type="xs:string"/>
</xs:complexType>
</xs:element>
<!-- propertyDescriptionList -->
<xs:element name="propertyDescriptionList">
<xs:complexType>
<xs:sequence>
<xs:element ref="propertyDescription" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="publisher" type="xs:string"/>
<xs:attribute name="product" type="xs:string"/>
</xs:complexType>
</xs:element>
<!-- propertyDescription -->
<xs:element name="propertyDescription">
<xs:complexType>
<xs:all>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element ref="searchInfo" minOccurs="1" maxOccurs="1"/>
<xs:element ref="labelInfo" minOccurs="0" maxOccurs="1"/>
<xs:element ref="typeInfo" minOccurs="0" maxOccurs="1"/>
<xs:element ref="displayInfo" minOccurs="0" maxOccurs="1"/>
</xs:all>
<!--<xs:attribute name="formatID" type="upcase-uuid" use="required"/>-->
<xs:attribute name="formatID" type="xs:string" use="required"/>
<xs:attribute name="propID" type="xs:nonNegativeInteger" use="required"/>
<!--<xs:attribute name="name" type="canonical-name" use="required"/>-->
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<!-- displayInfo -->
<xs:element name="displayInfo">
<xs:complexType>
<xs:all>
<xs:element name="stringFormat" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="formatAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="General"/>
<xs:enumeration value="FileName"/>
<xs:enumeration value="FilePath"/>
<xs:enumeration value="Hyperlink"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="booleanFormat" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="formatAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="YesNo"/>
<xs:enumeration value="OnOff"/>
<xs:enumeration value="TrueFalse"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="numberFormat" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="formatAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="General"/>
<xs:enumeration value="Percentage"/>
<xs:enumeration value="ByteSize"/>
<xs:enumeration value="KBSize"/>
<xs:enumeration value="SampleSize"/>
<xs:enumeration value="Bitrate"/>
<xs:enumeration value="SampleRate"/>
<xs:enumeration value="FrameRate"/>
<xs:enumeration value="Pixels"/>
<xs:enumeration value="DPI"/>
<xs:enumeration value="Duration"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="formatDurationAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="hh:mm"/>
<xs:enumeration value="hh:mm:ss"/>
<xs:enumeration value="hh:mm:ss.fff"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="dateTimeFormat" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="formatAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="General"/>
<xs:enumeration value="Month"/>
<xs:enumeration value="YearMonth"/>
<xs:enumeration value="Year"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="formatTimeAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ShortTime"/>
<xs:enumeration value="LongTime"/>
<xs:enumeration value="HideTime"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="formatDateAs">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ShortDate"/>
<xs:enumeration value="LongDate"/>
<xs:enumeration value="HideDate"/>
<xs:enumeration value="RelativeShortDate"/>
<xs:enumeration value="RelativeLongDate"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="enumeratedList" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="enum" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string" use="required"/>
<xs:attribute name="text" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="enumRange" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="minValue" type="xs:string" use="required"/>
<xs:attribute name="setValue" type="xs:string"/>
<xs:attribute name="text" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="defaultText" type="xs:string"/>
<xs:attribute name="useValueForDefault" type="xs:boolean"/>
</xs:complexType>
</xs:element>
<xs:element name="drawControl" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="control">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Default"/>
<xs:enumeration value="MultiLineText"/>
<xs:enumeration value="MultiValueText"/>
<xs:enumeration value="PercentBar"/>
<xs:enumeration value="ProgressBar"/>
<xs:enumeration value="Rating"/>
<xs:enumeration value="StaticText"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="editControl" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="control">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Default"/>
<xs:enumeration value="Calendar"/>
<xs:enumeration value="CheckboxDropList"/>
<xs:enumeration value="DropList"/>
<xs:enumeration value="MultiLineText"/>
<xs:enumeration value="MultiValueText"/>
<xs:enumeration value="Rating"/>
<xs:enumeration value="Text"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="filterControl" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="control">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Default"/>
<xs:enumeration value="Calendar"/>
<xs:enumeration value="Rating"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="queryControl" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="control">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Default"/>
<xs:enumeration value="Boolean"/>
<xs:enumeration value="Calendar"/>
<xs:enumeration value="CheckboxDropList"/>
<xs:enumeration value="DropList"/>
<xs:enumeration value="MultiValueText"/>
<xs:enumeration value="NumericText"/>
<xs:enumeration value="Rating"/>
<xs:enumeration value="Text"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="defaultColumnWidth" type="xs:nonNegativeInteger" default="20"/>
<xs:attribute name="displayType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="String"/>
<xs:enumeration value="Number"/>
<xs:enumeration value="Boolean"/>
<xs:enumeration value="DateTime"/>
<xs:enumeration value="Enumerated"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="alignment" default="Left">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Left"/>
<xs:enumeration value="Center"/>
<xs:enumeration value="Right"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="relativeDescriptionType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="General"/>
<xs:enumeration value="Date"/>
<xs:enumeration value="Size"/>
<xs:enumeration value="Count"/>
<xs:enumeration value="Revision"/>
<xs:enumeration value="Length"/>
<xs:enumeration value="Duration"/>
<xs:enumeration value="Speed"/>
<xs:enumeration value="Rate"/>
<xs:enumeration value="Rating"/>
<xs:enumeration value="Priority"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="defaultSortDirection" default="Ascending">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Ascending"/>
<xs:enumeration value="Descending"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<!-- searchInfo for Windows 7-->
<xs:element name="searchInfo">
<xs:complexType>
<xs:attribute name="inInvertedIndex" type="xs:boolean" default="false"/>
<xs:attribute name="isColumn" type="xs:boolean" default="false"/>
<xs:attribute name="isColumnSparse" type="xs:boolean" default="true">
<xs:annotation>
<xs:documentation>
isColumnSparse: Default is true. If the property is multi-valued, this is always true.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="columnIndexType" default="OnDemand">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="NotIndexed"/>
<xs:enumeration value="OnDisk"/>
<xs:enumeration value="OnDiskAll"/>
<xs:enumeration value="OnDiskVector"/>
<xs:enumeration value="OnDemand"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="maxSize" type="xs:nonNegativeInteger" default="512"/>
<xs:attribute name="mnemonics" type="xs:string"/>
</xs:complexType>
</xs:element>
<!-- labelInfo -->
<xs:element name="labelInfo">
<xs:complexType>
<xs:attribute name="label" type="xs:string"/>
<xs:attribute name="sortDescription">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="General"/>
<xs:enumeration value="AToZ"/>
<xs:enumeration value="LowestHighest"/>
<xs:enumeration value="OldestNewest"/>
<xs:enumeration value="SmallestLargest"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="invitationText" type="xs:string"/>
<xs:attribute name="hideLabel" type="xs:boolean" default="false"/>
</xs:complexType>
</xs:element>
<!-- typeInfo for Windows 7-->
<xs:element name="typeInfo">
<xs:complexType>
<xs:attribute name="type" default="Any">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Any"/>
<xs:enumeration value="Null"/>
<xs:enumeration value="String"/>
<xs:enumeration value="Boolean"/>
<xs:enumeration value="Byte"/>
<xs:enumeration value="Buffer"/>
<xs:enumeration value="Int16"/>
<xs:enumeration value="UInt16"/>
<xs:enumeration value="Int32"/>
<xs:enumeration value="UInt32"/>
<xs:enumeration value="Int64"/>
<xs:enumeration value="UInt64"/>
<xs:enumeration value="Double"/>
<xs:enumeration value="DateTime"/>
<xs:enumeration value="Guid"/>
<xs:enumeration value="Blob"/>
<xs:enumeration value="Stream"/>
<xs:enumeration value="Clipboard"/>
<xs:enumeration value="Object"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="groupingRange">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Discrete"/>
<xs:enumeration value="Alphanumeric"/>
<xs:enumeration value="Size"/>
<xs:enumeration value="Date"/>
<xs:enumeration value="Dynamic"/>
<xs:enumeration value="Percent"/>
<xs:enumeration value="Enumerated"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="isInnate" type="xs:boolean" default="false"/>
<xs:attribute name="canBePurged" type="xs:boolean"/>
<xs:attribute name="multipleValues" type="xs:boolean" default="false"/>
<xs:attribute name="isGroup" type="xs:boolean" default="false"/>
<xs:attribute name="aggregationType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Default"/>
<xs:enumeration value="First"/>
<xs:enumeration value="Sum"/>
<xs:enumeration value="Average"/>
<xs:enumeration value="DateRange"/>
<xs:enumeration value="Union"/>
<xs:enumeration value="Maximum"/>
<xs:enumeration value="Minimum"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="isTreeProperty" type="xs:boolean" default="false"/>
<xs:attribute name="isViewable" type="xs:boolean" default="false"/>
<xs:attribute name="isQueryable" type="xs:boolean" default="false"/>
<xs:attribute name="includeInFullTextQuery" type="xs:boolean" default="false"/>
<xs:attribute name="searchRawValue" type="xs:boolean" default="false"/>
<xs:attribute name="conditionType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="None"/>
<xs:enumeration value="String"/>
<xs:enumeration value="Number"/>
<xs:enumeration value="DateTime"/>
<xs:enumeration value="Boolean"/>
<xs:enumeration value="Size"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="defaultOperation">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Equal"/>
<xs:enumeration value="NotEqual"/>
<xs:enumeration value="LessThan"/>
<xs:enumeration value="GreaterThan"/>
<xs:enumeration value="Contains"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
I have a XSD definition which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ARTICLES">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="ARTICLE">
<xs:complexType>
<xs:sequence>
<xs:element name="NAME" type="xs:string" />
<xs:element name="TYPE" type="xs:unsignedByte" />
<xs:element name="LENGTH" type="xs:unsignedByte" />
<xs:element name="WIDTH" type="xs:unsignedShort" />
<xs:element name="HEIGHT" type="xs:unsignedShort" />
<xs:element maxOccurs="unbounded" name="PRICELIST">
<xs:complexType>
<xs:sequence>
<xs:element name="SALESCONTRACT" type="xs:string" />
<xs:element name="PRICE" type="xs:decimal" />
<xs:element name="VAT" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
An XML can contain a collection of ATRICLES and each ARTICLE can have 1 or more PRICELIST entries
Now I would like to take an element and restrict the value entries. i.e. With a regex or numbers only or in length.
With simpletype I found lots of examples that work with a pattern tag.
How would this work with complex types?
Regards,
Rick
You can simply apply all of those examples you've found using xs:simpleType to any of the parts of ARTICLE that you wish to further constrain. For example, to limit NAME to be of maximum length of 32 characters:
<xs:element name="NAME">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I'm trying to make just one adapter for every type of element, so I created a bindings.xml file:
<jxb:bindings node="//xs:attribute[#type='Id']"
so, my intention is to address to every attribute of type "Id".
Problem is that xjc tells me "too many target nodes(3)" ... but it's just what I want!!
Try to add multiple="true" attribute:
<jxb:bindings multiple="true" node="//xs:attribute[#type='Id']"
I end up with a similar problem "too many target nodes(3)" however could not find any answer on any of the sites...Posting the solution which I found after lots of trail and error...Basic idea to solve "too many target nodes(3)" is to give complete XPATH of the node which is multiple in your XSD.
Below is my XSD:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element name="asset">
<xs:complexType>
<xs:sequence>
<xs:element name="attribute" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="string" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="date" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="array" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="struct" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="field" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:byte" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="assetreference" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="type"/>
<xs:attribute type="xs:long" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:long" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="file" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:long" name="id"/>
<xs:attribute type="xs:string" name="type"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and below is the JAXB binding file which is working for above XSD:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation= "../assetproduct.xsd" version="1.0">
<!-- Customise the package name
<schemaBindings>
<package name="com.example.schema"/>
</schemaBindings> -->
<!-- rename the value element -->
<bindings node="//xs:element[#name='document']">
<bindings node="//xs:element[#name='asset']">
<bindings node="//xs:element[#name='attribute']">
<bindings node="//xs:element[#name='string']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='date']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='array']">
<bindings node=".//xs:element[#name='struct']">
<bindings node=".//xs:element[#name='field']">
<bindings node=".//xs:element[#name='integer']/xs:complexType">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node=".//xs:element[#name='assetreference']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
<bindings node=".//xs:element[#name='array']/xs:complexType/xs:sequence/xs:element[#name='integer']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='attribute']/xs:complexType/xs:sequence/xs:element[#name='integer']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child name="MyType" compareMode="EQ">Child1</Child>
</Root>
Usually in order to verify such an xml one would use the following xml schema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I want to limit values of a Child element and allow only the following: Child1, Child2 and Child3.
I know that usually restriction may be specified with the following schema:
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
Which restriction is correct in the first case?
Does this answer your question?
Having both an attribute and a restriction on an element in xml schema
I was wrong, because I didn't test my answer. I edited it:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ChildContent">
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="ChildContent">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" type="Child" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
or with restriction:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="Child">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
After hours spent with google: You cannot do this without named types.
Hi guys I have a simple XML file with this structure
... ...
<Fields>
<Field name="MainJob.Id" value="t066_id">
<Description nullable="false" type="System.Int32" />
</Field>
What I have actually is this XSD file description:
<xs:element minOccurs="0" maxOccurs="unbounded" name="Fields">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Field">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Description">
<xs:complexType>
<xs:attribute name="nullable" type="xs:string" use="required" /> <xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="minLength" type="xs:string" use="optional" />
<xs:attribute name="maxLength" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
How I can define two only available values for the attribute nullable, like 'true' and 'false'?
If I nest a SimpleType inside the attribute the .XSD file is not valid anymore.
thank you
Type should be not xs:string but xs:boolean for your example, or you can use enumeration (example).
You can add a simpleType like so:
<xs:simpleType name="TrueOrFalse">
<xs:restriction base="xs:string">
<xs:enumeration value="false"/>
<xs:enumeration value="true"/>
</xs:restriction>
</xs:simpleType>
and then change your nullable to:
<xs:attribute name="nullable" type="TrueOrFalse" use="required" />
Marc