2014-02-06 4 views
0

요소의 선택적 특성을 사용하는 방법에 대한 내 XSD 스키마 정의 내에 규칙을 포함하고 싶습니다. 다음 요소 정의를 고려하십시오.XSD 스키마 내에서의 유효성 검사

<xs:complexType name="sampleElement"> 
    <xs:attribute name="name" type="xs:string" use="required"/> 
    <xs:attribute name="description" type="xs:string" use="optional"/> 
    <xs:attribute name="optPrimary" type="xs:string" use="optional"/> 
    <xs:attribute name="optSecondary" type="xs:string" use="optional"/> 
</xs:complexType 

이 예제에서 optPrimary와 optSecondary는 실제로 선택 사항입니다. optPrimary 속성은 단독으로 사용할 수 있지만 optSecondary는 optPrimary와 함께 사용해야합니다. 따라서 XML 유효성을 검사 할 때 적용 할 수있는 스키마에 포함 된 규칙을 원합니다.

이 파일은 Schematron의 예제를 별도의 파일에서 찾았지만 스키마의 일부로 포함시킬 수있는 방법을 찾지 못했습니다.

+0

XML 스키마는 요소간에 제약 조건을 생성하는 기능을 지원하지 않습니다. 이 차이를 해결하는 스키마 트론을 정확히 확인했습니다. –

답변

0

XSD 1.1이 지원합니다. 이는 표현 방법 중 하나입니다.

<?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" xmlns:xerces="http://xerces.apache.org"> 
    <xsd:element name="sampleElement"> 
     <xsd:complexType> 
      <xsd:attribute name="name" type="xsd:string" use="required"/> 
      <xsd:attribute name="description" type="xsd:string" use="optional"/> 
      <xsd:attribute name="optPrimary" type="xsd:string" use="optional"/> 
      <xsd:attribute name="optSecondary" type="xsd:string" use="optional"/> 
      <xsd:assert test="@optPrimary or not(@optSecondary)" xerces:message="Secondary needs primary..."/> 
     </xsd:complexType>   
    </xsd:element> 
</xsd:schema>