2011-12-16 12 views
1

私自身のComplexType - Transactionを作成しました。カスタムコンプレックスタイプの属性/値を設定します

私は、呼び出し元の要素からその複合型の属性をデフォルトで設定したいと考えています。例えば

:デビットについて

<xs:complexType name="TransactionType"> 
    <xs:all minOccurs="0" maxOccurs="1"> 
    <xs:element name="Status" type="xs:string" minOccurs="1" fixed ="N" maxOccurs ="1" /> 
    <xs:element name="Amount" type="xs:decimal" minOccurs ="1" maxOccurs ="1" /> 
    </xs:all> 
    <xs:attribute name="type" type="xs:string" /> 
</xs:complexType> 

<xs:element name="Transaction" maxOccurs="unbounded"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="Debit" type="TransactionType" /> 
     <xs:element name="Credit" type="TransactionType" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 

iは=「D」固定値TransactionType.Typeしたいと思う、クレジット要素の私はそれがtransactionType.Type =「C」になりたいです

おかげ

あなたは、あなたがそれを言葉で表現しました方法を行うことができない何をしたいです

答えて

1

。私は可能性を説明するために、別の方法を示します。

何があっても、新しいタイプを使用する必要があります。

最初のアプローチでは、アトリビュートのないパーティクルではなく、すべてのパーティクルとアトリビュートをベースタイプにすることを前提としています(後者のソリューションは、制限の)。ここでの制限がなぜ役立たないのかについては、XSDを見て理解しやすいはずです。

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)--> 
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="TransactionType"> 
     <xs:all minOccurs="0" maxOccurs="1"> 
      <xs:element name="Status" type="xs:string" minOccurs="1" fixed ="N" maxOccurs ="1"/> 
      <xs:element name="Amount" type="xs:decimal" minOccurs ="1" maxOccurs ="1"/> 
     </xs:all> 
     <xs:attribute name="type" type="xs:string"/> 
    </xs:complexType> 
    <xs:complexType name="DebitTransactionType"> 
     <xs:complexContent> 
      <xs:restriction base="TransactionType"> 
       <xs:all> 
        <xs:element name="Status" type="xs:string" minOccurs="1" fixed ="N" maxOccurs ="1"/> 
        <xs:element name="Amount" type="xs:decimal" minOccurs ="1" maxOccurs ="1"/>     
       </xs:all> 
       <xs:attribute name="type" type="xs:string" fixed="D"/>     
      </xs:restriction> 
     </xs:complexContent> 
    </xs:complexType> 
    <xs:complexType name="CreditTransactionType"> 
     <xs:complexContent> 
      <xs:restriction base="TransactionType"> 
       <xs:all> 
        <xs:element name="Status" type="xs:string" minOccurs="1" fixed ="N" maxOccurs ="1"/> 
        <xs:element name="Amount" type="xs:decimal" minOccurs ="1" maxOccurs ="1"/>     
       </xs:all> 
       <xs:attribute name="type" type="xs:string" fixed="C"/>     
      </xs:restriction> 
     </xs:complexContent> 
    </xs:complexType> 
    <xs:element name="Transaction"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Debit" type="DebitTransactionType"/> 
       <xs:element name="Credit" type="CreditTransactionType"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

有効なXML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <Debit type="D"> 
     <Amount>1</Amount> 
     <Status>N</Status> 
    </Debit> 
    <Credit type="C"> 
     <Amount>1</Amount> 
     <Status>N</Status> 
    </Credit> 
</Transaction> 

事制限とは、いくつかは、そのエレガントではないと言うでしょう...あなたはセット全体を「リピート」しなければならないということです。

基本タイプから属性を削除できる場合は、もう1つの方法として、拡張子を使用します。

<?xml version="1.0" encoding="utf-8" ?> 
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)--> 
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="TransactionType"> 
     <xs:all minOccurs="0" maxOccurs="1"> 
      <xs:element name="Status" type="xs:string" minOccurs="1" fixed ="N" maxOccurs ="1"/> 
      <xs:element name="Amount" type="xs:decimal" minOccurs ="1" maxOccurs ="1"/> 
     </xs:all> 
    </xs:complexType> 
    <xs:complexType name="DebitTransactionType"> 
     <xs:complexContent> 
      <xs:extension base="TransactionType"> 
       <xs:attribute name="type" type="xs:string" fixed="D"/>     
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
    <xs:complexType name="CreditTransactionType"> 
     <xs:complexContent> 
      <xs:extension base="TransactionType"> 
       <xs:attribute name="type" type="xs:string" fixed="C"/>     
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
    <xs:element name="Transaction"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Debit" type="DebitTransactionType"/> 
       <xs:element name="Credit" type="CreditTransactionType"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

...それは難しい要件ではない場合、私は属性を削除します...あなたは属性の値が固定され、要素の名前に結び付けられているので、あなたには、いくつかの冗長を持っているようです
関連する問題