2013-07-28 2 views
1

SAX 파서를 사용하여 XML 유효성 검사에 몇 가지 문제가 있습니다. 여기에 내가 문제를 제시 만든 간단한 XML 스키마입니다 :XML 스키마 : complexType의 속성 정의 사용

<?xml version="1.0"?> 
<xs:schema targetNamespace="urn:test" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" 
    xmlns="urn:test"> 

    <xs:element name="root"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="content" type="ContentType" 
        maxOccurs="unbounded" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:complexType name="ContentType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute ref="title" use="required" /> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <xs:attribute name="title" type="xs:string" /> 

</xs:schema> 

는 그리고 여기 내 스키마에 관해서 유효해야 내 생각에 꽤 간단한 XML 파일입니다

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<root xmlns="urn:test"> 
     <content title="Title"> 
      Content comes here... 
     </content> 
</root> 

흥미로운를 것은 내가이 XML 파일을 구문 분석 할 때, 나는 다음과 같은 유효성 검사 오류가 나타날 것입니다 :

cvc-complex-type.3.2.2: Attribute 'title' is not allowed to appear in element 'content'.

을하지만 attri을 제목을 제거하는 경우

cvc-complex-type.4: Attribute 'title' must appear on element 'content'.

나는 문제가 무엇인지 전혀 모른다 : XML 파일에서 내용 요소의 뷰트, 나는 여전히 유효성 검사 오류를받을 수 있습니다. 물론, 이것은 문제를 제시하는 단순한 예일뿐입니다. 나는이 행동의 원인을 이해하고 싶다. 또한 해결책을 찾는 것이 좋을 것입니다. 이 경우 Java 코드의 유효성 검사가 중요한지 여부는 확실하지 않지만 필요한 경우 나중에 게시 할 예정입니다.

도움을 주시면 감사하겠습니다.

답변

2

title 특성의 전역 선언은 해당 특성을 대상 네임 스페이스 urn:test에 넣습니다. 즉, 스키마 및 인스턴스 문서에서 속성에 대한 참조를 한정해야한다는 의미이기도합니다. 기본적으로 규정되지 않은 속성에는 이름 공간이 없습니다.

<xs:schema targetNamespace="urn:test" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" 
xmlns="urn:test" xmlns:test="urn:test" > 
....  
<xs:complexType name="ContentType"> 
    <xs:simpleContent> 
     <xs:extension base="xs:string"> 
      <xs:attribute ref="test:title" use="required" /> 
     </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 

<xs:attribute name="title" type="xs:string" /> 

<root xmlns="urn:test" xmlns:test="urn:test" > 
    <content test:title="Title"> 
     Content comes here... 
    </content> 
</root> 

이 모든 것은 아주 미묘 내가 ecplise의 원본 인스턴스 문서의 유효성을 검사 할 때 나는이 매우 혼란 오류를 얻을 :

  1. title 속성 을 콘텐츠 요소에을 표시 할 수 없습니다. 이는 특성의 규정되지 않은 사용을 나타내며
  2. title 요소 은 내용 요소에이 나타나야합니다. 이 값은 누락 된 정규화 된 test:title 속성을 나타냅니다.

허용됨, 오류 메시지는 더 많은 컨텍스트 정보를 사용할 수 있습니다.

+0

네, 이해합니다. 답변 감사합니다. 나는 가능한 한 전역 속성 유형 정의를 피할 것이라고 생각한다. 이러한 정의를 참조하기 위해 정규화 된 이름을 사용하는 것은 XML 스키마에서 문제가되지 않지만 XML 데이터 파일에서 네임 스페이스 설명자와 함께 특성 이름을 사용하고 싶지 않습니다. –