2016-05-09 9 views
4

固定値属性の一定の値を生成します。JAXB:私は現在、次のcontructを使用して、XSDに取り組んでいます

<xs:attribute name="listVersionID" type="xs:normalizedString" use="required" fixed="1.0"> 

それ自体は問題ではない、以来、で動作するようにかなりうるさいですがこの定義の固定値はxsd仕様のリリース間で増加し、xsdの興味のあるものが変更されてもほとんど変わらないが、それらを有効に保つために別の定数クラスの値を変更する必要がある。 xsdは他の場所でも管理されているので、変更するだけでオプションはありません。

JAXB、プラグインや固定値を手動で移入する必要があります

@XmlAttribute(name = "listVersionID") 
@XmlJavaTypeAdapter(NormalizedStringAdapter.class) 
@XmlSchemaType(name = "normalizedString") 
protected final String listVersionID = "1.0"; 

だけではなく

@XmlAttribute(name = "listVersionID") 
@XmlJavaTypeAdapter(NormalizedStringAdapter.class) 
@XmlSchemaType(name = "normalizedString") 
protected String listVersionID; 

たala定数に属性をオンにすると類似がある天気をこのように私は自分自身を求めていました。

誰も知っていますか?

答えて

3

はいカスタムjaxbバインディングを使用すると、codegenでファイルとして追加できます。

jaxbバインディングには、fixedAttributeAsConstantProperty属性があります。これをtrueに設定すると、fixed属性を持つ属性をjava-constantとして生成するようにコードジェネレータに指示します。

グローバルバインディングを通じ1: ローカルマッピングを通じて

<schema targetNamespace="http://stackoverflow.com/example" 
     xmlns="http://www.w3.org/2001/XMLSchema" 
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     jaxb:version="2.0"> 
    <annotation> 
    <appinfo> 
     <jaxb:globalBindings fixedAttributeAsConstantProperty="true" /> 
    </appinfo> 
    </annotation> 
    ... 
</schema> 

2を定数に固定値ですべての属性を作る:

は、このために2つのオプションがあります これは、特定の属性に対してのみfixedAttributeAsConstantPropertyプロパティを定義します。

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

    <jaxb:bindings schemaLocation="yourschema.xsd" node="/xs:schema"> 
    <jaxb:globalBindings fixedAttributeAsConstantProperty="true" /> 
    </jaxb:bindings> 

</jaxb:bindings> 

:あなたはあなたのスキーマを変更したくない場合は、別のオプションは、外部結合ファイルを使用することです

@XmlRootElement(name = "example") 
public class Example { 
    @XmlAttribute 
    public final static int SOMECONSTANT = 42; 
} 
3

<schema targetNamespace="http://stackoverflow.com/example" 
     xmlns="http://www.w3.org/2001/XMLSchema" 
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     jaxb:version="2.0"> 
    <complexType name="example"> 
     <attribute name="someconstant" type="xsd:int" fixed="42"> 
      <annotation> 
       <appinfo> 
        <jaxb:property fixedAttributeAsConstantProperty="true" /> 
       </appinfo> 
      </annotation> 
     </attribute> 
    </complexType> 
    ... 
</schema> 

どちらの例も生じるはずです彼の答えに@jmattheisを提案することと同等です。

関連する問題