2011-09-02 4 views
2

하나의 단일 루트 노드 만 허용하는 xml 스키마를 만들고 싶습니다.XML 스키마의 전역 요소 사용을 피하는 방법

루트 노드 아래 구조에는 다른 위치에서 다시 사용하려는 요소가 있습니다.

첫 번째 방법은 스키마에 전역 요소를 만드는 것이지만, 이렇게하면 루트 태그 만있는 XML 문서도이 스키마에 대해 유효합니다.

루트 구조 내에서 ref 요소로만 사용할 수있는 전역 요소를 어떻게 만들 수 있습니까?

이 내가해야 할 것입니다 :

<root> 
    <branch> 
    <leaf/> 
    </branch> 
    <branch> 
    <fork> 
     <branch> 
      <leaf/> 
     </branch> 
     </leaf> 
    </fork> 
</root> 

을하지만이 또한 루트 노드로 <leaf/> 유효한 것

답변

0

XML은 항상 하나 개의 루트 노드가 있습니다. 이것은 계층 적 구조를 나타내며 스키마에 바인딩됩니다. 따라서 동일한 스키마와 유효한 루트 요소를 변경할 수 없습니다.

는 처음에는이 같은 잘 형성되어야한다

<?xml version="1.0" encoding="UTF-8"?> 

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="your-scheme.xsd> 
    <branch> 
     <leaf/> 
    </branch> 
    <branch> 
     <fork> 
      <branch> 
       <leaf/> 
      </branch> 
      <leaf/> 
     </fork> 
    </branch> 
</root> 

나는이 같은 계획을 제안 :

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="root"> 
     <xs:sequence> 
      <xs:element ref="branch" minOccurs="0" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="branch"> 
     <xs:choice> 
      <xs:element ref="fork" minOccurs="0" maxOccurs="unbounded"/> 
      <xs:element ref="leaf" minOccurs="0" maxOccurs="unbounded"/> 
     </xs:choice> 
    </xs:complexType> 
    <xs:complexType name="fork"> 
     <xs:sequence> 
      <xs:element ref="branch" minOccurs="0" maxOccurs="unbounded"/> 
      <xs:element ref="leaf" minOccurs="0" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="leaf"/> 
    <xs:element name="root" type="root"/> 
    <xs:element name="branch" type="branch"/> 
    <xs:element name="fork" type="fork"/> 
    <xs:element name="leaf" type="leaf"/> 
</xs:schema> 

를 내가 당신을 도움이되기를 바랍니다.

+0

글쎄, XML 데이터의 단일 내용으로 리프 태그를 사용하지 못하게하는 이유는 무엇입니까? – martin

관련 문제