2012-03-30 19 views
34

Chcę poznać poprawną składnię do definiowania elementu zawierającego zarówno atrybuty, jak i elementy potomne (jeśli to możliwe). Na przykład:Element XSD z atrybutami i elementami potomnymi

<component type="A" binding="B"> 
    <operation name="X"> 
    <input type="C" /> 
    </operation> 

    <event name="Y"> 
    <output type="D" /> 
    </event> 
</component> 

Jak widać, zarówno element, działanie, jak i zdarzenie mają zarówno atrybuty, jak i element potomny. Czy można to zdefiniować w XSD? W jaki sposób?

Dziękuję bardzo!

Odpowiedz

35

Jest to jeden z możliwych sposobów zdefiniowania XSD pasującego do Twojego XML; kiedy uczysz się XSD, możesz zarejestrować pomoc narzędzia, które zawiera dla Ciebie XSD, zaczynając od jednego lub więcej przykładowych plików XML.

<?xml version="1.0" encoding="utf-8"?> 
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="component"> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element name="operation"> 
      <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="input"> 
       <xsd:complexType> 
        <xsd:attribute name="type" type="xsd:string" use="required" /> 
       </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
      <xsd:attribute name="name" type="xsd:string" use="required" /> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="event"> 
      <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="output"> 
       <xsd:complexType> 
        <xsd:attribute name="type" type="xsd:string" use="required" /> 
       </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
      <xsd:attribute name="name" type="xsd:string" use="required" /> 
      </xsd:complexType> 
     </xsd:element> 
     </xsd:sequence> 
     <xsd:attribute name="type" type="xsd:string" use="required" /> 
     <xsd:attribute name="binding" type="xsd:string" use="required" /> 
    </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

Jak solucja to, można zacząć szczypanie min/maxOccurs, korzystanie (wymagane/opcjonalne) definicje ponownego użycia, itp generowane XSD jest dobrym punktem wyjścia, ale zazwyczaj kończy się jedną lub edytowane inny ...

+24

myślę, że wymaga to 'xsd: sequence' przychodzi przed' xsd: attribute's. –

+5

Tak, dokładnie przeciwnie do tego, czego można się spodziewać, biorąc pod uwagę, że w faktycznym xml atrybuty pojawiają się przed elementami. –

+4

Zła decyzja projektantów od projektantów XML Schema sprawia, że ​​XSD jest trudniejsze do odczytania, poza tym, że jest intuicyjny. Powinien móc umieścić atrybut i sekwencję itp. W dowolnej kolejności wewnątrz complexType –

3

podaję poniżej, rozwiązanie, które działa:

<xs:simpleType name="inputTypeType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:complexType name="inputType"> 
    <xs:attribute name="type" type="inputTypeType"/>    
</xs:complexType> 

<xs:simpleType name="operationNameType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:complexType name="operationType"> 
    <xs:sequence> 
    <xs:element name="input" type="inputType" /> 
    </xs:sequence> 
    <xs:attribute name="name" type="operationNameType"/> 
</xs:complexType> 



<xs:simpleType name="outputTypeType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:complexType name="outputType"> 
    <xs:attribute name="type" type="outputTypeType"/>   
</xs:complexType> 

<xs:simpleType name="eventNameType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:complexType name="eventType"> 
    <xs:sequence> 
    <xs:element name="output" type="outputType" /> 
    </xs:sequence> 
    <xs:attribute name="name" type="eventNameType"/> 
</xs:complexType> 


<xs:simpleType name="typeType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:simpleType name="bindingType"> 
     <xs:restriction base="xs:string" /> 
</xs:simpleType> 




<xs:complexType name="componentType"> 
    <xs:sequence>   
     <xs:element name="operation" type="operationType" /> 
     <xs:element name="event" type="eventType" /> 
    </xs:sequence> 
    <xs:attribute name="type" type="typeType"/> 
    <xs:attribute name="binding" type="bindingType"/>  
</xs:complexType> 


<xs:element name="component" type="componentType" /> 

Powiązane problemy