2010-05-11 6 views
3

또 다른 XSD 질문은 - 어떻게 다음과 같은 XML 요소가 모두 유효하다는 것을 얻을 수 있습니다다른 하위 요소는

<some-element> 
    <type>1</type> 
    <a>...</a> 
</some-element> 

<some-element> 
    <type>2</type> 
    <b>...</b> 
</some-element> 

하위 요소 (중 하나<a>또는<b>을)은 <type>의 내용 (특성 일 수도 있음)에 따라 달라야합니다. 그것은 RelaxNG 그렇게 간단 할 것이다 - 그러나 RelaxNG는

는 XSD에서이를 구현하는 방법이 있나요 키 무결성을 :(지원하지 않습니다

참고 : XML 스키마 버전 1.1 <xs:alternative>을 지원하는 솔루션이 될 수있는? .,하지만 참조 구현 (예 : libxml2를) AFAIK 그래서 해결 방법을 찾고 있어요 아직 지원 내가 함께 왔어요 유일한 방법은 다음과 같습니다..

<type>1</type> 
<some-element type="1"> 
    <!-- simple <xs:choice> between <a> and <b> goes here --> 
    <a>...</a> 
</some-element> 
<!-- and now create a keyref between <type> and @type --> 

답변

2

아니, XML 스키마 1.0이 작업을 수행 할 수 없습니다

3

가장 좋은 해결책은 <type/> 요소이며 <a/><b/>에 대해서만 xs:choice이며 xml을 소비하는 응용 프로그램에서 유형을 분류하도록합니다. <a/><b/> 관련하여 <type/> 요소의 유효성 검사를 수행하는 XSLT 스크립트를 사용하십시오 xs:choice<a/><b/>

또 다른 해결책은 가질 수 있습니다.

먼저 xmlschema에 대해 xml의 유효성을 검사 한 다음 xslt를 사용하여 변형을 수행합니다. 변환 결과가 빈 문자열이면 유효하고 그렇지 않으면 결과 문자열을 오류 메시지로 표시합니다.

이런 식으로 뭔가 ...

에 XmlSchema :

<xs:element name="some-element"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="type" type="xs:integer" /> 
     <xs:choice> 
      <xs:element name="a" type="xs:string" /> 
      <xs:element name="b" type="xs:string" /> 
     </xs:choice> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

XSLT :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:demo="uri:demo:namespace"> 
    <xsl:output method="text" /> 
    <xsl:template match="/demo:some-element"> 
    <xsl:if test="type = 1 and not(demo:a)"> 
     When type equals 1 element a is requred. 
    </xsl:if> 
    <xsl:if test="type = 2 and not(demo:b)"> 
     When type equals 2 element b is requred. 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
관련 문제