Java XML - xsd 파일에서 attributeGroup

2023. 1. 17. 21:15Java

프로젝트 내 group02.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="도서리스트" type="책리스트"/>
    
    <xs:complexType name="책리스트">
        <xs:sequence>
            <xs:element name="책" type="책1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="책1">
        <xs:sequence>
            <xs:element name="제목" type="xs:string"/>
            <xs:element name="저자" type="person"/>
            <xs:element name="출판사" type="publishInfo"/><!-- publishInfo 타입으로 설정 
            디자인을 통해 확인 한다. 디자인에 나온 요 구조대로 여러분들이 스키마를 만든다면 
            schema 오른쪽마우스버튼, generate sample xml files
            file name prefix 에서 group02로 지정하고 xml을 하나 
            생성한다.(xml 인스턴스 = xml 파일 인스턴스 한개 생성) -->
        </xs:sequence>
    </xs:complexType>
    
    <!-- 저자는 사람이라 사람이라는 엘리먼트하나 생성 -->
    
    <xs:complexType name="person">
        <xs:attributeGroup ref="Info"/><!-- 그룹엘리먼트만 추가 -->
    </xs:complexType>
    
    <!-- 제한 확장 조건으로 속성을 사용할 수 있겠지만 그룹이 정해졌으므로
    그룹 참조만 하면 된다 group을 출판사에서도 사용할 수 있도록
    컴플렉스 타잎을 선언해준다 출판사에 대한 정보 자식엘레먼트도 있지만
    밑에 attributeGoup personInfo를 고대로 사용하자는 거죠 -->
    <xs:complexType name="publishInfo">
        <xs:sequence>
            <!-- 자식 엘레먼트 두기 -->
            <!-- 회사에 대한 주소 -->
            <xs:element name="주소" type="xs:string"/>
        </xs:sequence>
        <!-- 밑에 attribute Group info를 고대로 활용한다 -->
        <xs:attributeGroup ref="Info"/>
    </xs:complexType><!-- 출판사에 대한 정보를 타입을 만들어 지정해 사용은 위에서 한다 
    type 이름은 publishInfo가 된다. -->
    
    <!-- 속성 그룹 attribute group 정의(설계) -->
    <!--
        출판사도 쓰려면 이름을 info로 바꾼다 
    <xs:attributeGroup name="personInfo">
    -->
    <xs:attributeGroup name="Info">
        <!-- 그 다음에 attribute 를 선언해 준다 
        요 속성을 일단 저자에서 사용할 수 있도록 하기 위해서 
        그룹에 대한 정의를 해야 된다 -->
        <xs:attribute name="name" type="xs:string"/>
        <xs:attribute name="전화번호" type="xs:string"/>
    </xs:attributeGroup>
    
</xs:schema>

프로젝트 내 group021.xml

<?xml version="1.0" encoding="UTF-8"?>
<도서리스트 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="file:/D:/javaddazua/test2/group02.xsd">
    <책>
        <제목>XML</제목>
        <저자 name="김말똥" 전화번호="010-2121-1241"/>
        <출판사 name="동아" 전화번호="02-454-5848">
            <주소>서울 강남구</주소>
        </출판사>
    </책>
    <!--
    <책>
        <제목></제목>
        <저자/>
        <출판사>
            <주소></주소>
        </출판사>
    </책>
    -->
</도서리스트>

XML > Validate