2013-03-12 4 views
1

xsd를 사용하여 xml 유효성을 검사하려고합니다.동일한 내부 구조를 가진 XSD 두 요소

xml은 개체를 만드는 데 사용됩니다. 목록의 요소로 작성할 수있는 오브젝트에는 SC와 SMSC의 두 가지 유형이 있습니다. SMSC는 SC이며이를 확장합니다.

SMSC에는 새로운 속성이 없습니다. XML의 관점에서 보면 SMSC는 속성을 정의하는 요소가 <SC> 태그 대신 <SMSC> 태그로 둘러싸여 있다는 점을 제외하면 모든면에서 SC와 동일합니다.

우리의 XSD는 다음과 같습니다 SMSC 요소의 속성 정의의 모든 복제 이외의 요소로 SC 또는 SMSC, 하나를 허용하려면이를 변경하는 방법은

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name='Definitions'> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element maxOccurs="unbounded" name="SC"> 
      <!--SNIP properties of SC and SMSC --> 
     </xsd:element> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

있습니까? 우리는 문서의 길이를 두 배로하고 모든 속성 정의를 복제하지 않아도됩니다.

우리가 XML에서 확인한 유일한 오류는 SMSC 요소가있는 곳입니다. 모든 속성 정의를 복제하지 않고이 문제를 해결할 방법이 없다면 그대로 두겠다.하지만 실용적이라면이 경고를 없애는 것이 좋습니다.

답변

1

by tags instead of tags이 혼란 스럽지만, 아래에 귀하의 질문에 대답하거나 더 나은 설명이 나온다고 생각합니다.

그래서, 중복을 피하는 것이 좋습니다. SMSC (Definitions2 참조)이 실제로 필요하지는 않지만 단지 (정의)에 넣었습니다. SMSC 요소를 SC 유형으로 만드는 것은 똑같이 작동합니다.

정의/Definitions2 및 Definitions3 하나 대신 선택의 치환 그룹을 사용한다는 차이점. 개인적으로 대체 그룹을 선택에 선호하지만 대체 그룹과 관련된 문제에 부딪히는 경우는 드뭅니다 (즉, 여기저기서 지원되지 않습니다).

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:complexType name="SC"> 
     <xsd:sequence> 
      <!-- Stuff goes here --> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="SMSC"> 
     <xsd:complexContent> 
      <xsd:extension base="SC"/> 
     </xsd:complexContent> 
    </xsd:complexType> 
    <xsd:element name="Definitions"> 
     <xsd:complexType> 
      <xsd:choice maxOccurs="unbounded"> 
       <xsd:element name="SC" type="SC"/> 
       <xsd:element name="SMSC" type="SMSC"/> 
      </xsd:choice> 
     </xsd:complexType> 
    </xsd:element> 
    <!-- Another way --> 
    <xsd:element name="Definitions2"> 
     <xsd:complexType> 
      <xsd:choice maxOccurs="unbounded"> 
       <xsd:element name="SC" type="SC"/> 
       <xsd:element name="SMSC" type="SC"/> 
      </xsd:choice> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:element name="Definitions3"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="SC" maxOccurs="unbounded"/>    
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:element name="SC" type="SC" />     
    <xsd:element name="SMSC" type="SMSC" substitutionGroup="SC" />    
</xsd:schema> 
+0

감사합니다. 나는 당신이 새로운 복합 유형을 정의 할 수 있고 그런 정의에서 사용할 수 있다는 것을 깨닫지 못했습니다. 매일 새로운 것을 배웁니다! (BTW, 첫 번째 버전은 지금 우리를 위해 일하고 있습니다) – Jeff

관련 문제