2011-12-16 43 views
1

我创建了我自己的ComplexType - 事务。设置自定义复杂类型的属性/值

我想从调用元素中默认设置该复杂类型的属性。

例如:

<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> 

对于借记我想具有固定值TransactionType.Type =“d”,而对于信用元件我想它是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> 

与限制的事情是,你要“重复”的整套...有些人会说没有那么优雅。

如果您可以从基本类型中删除属性,另一种方法是使用扩展名。

<?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> 

看来你有一些冗余,因为你有属性的值固定,绑元素...的名称。如果它不是一个硬性要求,我会删除属性...

相关问题