2012-01-28 5 views
0

XSDの日付の値はオプションですか? nillableを使って逃げる方法はありますか?例えばXSD属性はありますがオプションの値を持つ日付

、両方

<element attribute="attribute">optional-value</element> 
<element attribute="attribute"/> 

「はオプション値が」XSDのように定義されなければならない有効なタイプである:日付型。

答えて

3

はい、ではなく、何かでそのツールのような:

XSD:

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="root"> 
     <xsd:complexType> 
      <xsd:simpleContent> 
       <xsd:extension base="EmptyDate"> 
        <xsd:attribute name="attribute" type="xsd:string" use="required"/> 
       </xsd:extension> 
      </xsd:simpleContent> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:simpleType name="EmptyDate"> 
     <xsd:union memberTypes="xsd:date emptyString"/> 
    </xsd:simpleType> 

    <xsd:simpleType name="emptyString"> 
     <xsd:restriction base="xsd:string"> 
      <xsd:length value="0"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema> 

無効なXML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute="attribute1" xmlns="http://tempuri.org/XMLSchema.xsd"> </root> 

有効なXML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute="attribute1" xmlns="http://tempuri.org/XMLSchema.xsd"/> 
+0

私はこれをアンマーシャルJAXBを使用して、「オプション値」c omponentは、デフォルトのXML Date型(XMLGregorianCalendar)ではなくString型として非整列化されました。変更が必要なことはありますか? –

+0

これは、空の文字列のためにこの動作を得ているからです...これを望まないなら、あなたはnillableで立ち往生しています。 –

+0

ありがとうございました。私はJAXBガイドから、xs:union型をアンマーシャリングするためのデフォルトの動作がStringであり、他の型に変換する必要がプログラム的に処理される必要があることを発見しました –

関連する問題