ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • XML 스키마에 대해서
    컴공지식/웹 2024. 10. 12. 00:37

    일단 어떻게 생긴지 먼저 봐야하지 않겠나

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        
        <!-- 'note'라는 요소(Element)를 정의해. 이 요소는 복잡한 데이터 타입을 가질 것임. -->
        <xs:element name="note">
            
            <!-- 'note' 요소 안에 여러 요소들을 가질 수 있는 복합 타입을 정의함. -->
            <xs:complexType>
                
                <!-- 이 복합 타입은 여러 요소들이 순차적으로 등장하는 구조. -->
                <xs:sequence>
                    
                    <!-- 'to'라는 하위 요소는 문자열 타입. -->
                    <xs:element name="to" type="xs:string"/>
                    
                    <!-- 'from'이라는 하위 요소도 문자열 타입. -->
                    <xs:element name="from" type="xs:string"/>
                    
                    <!-- 'heading'이라는 하위 요소도 문자열 타입. -->
                    <xs:element name="heading" type="xs:string"/>
                    
                    <!-- 'body'라는 하위 요소도 문자열 타입. -->
                    <xs:element name="body" type="xs:string"/>
                    
                </xs:sequence>
                
            </xs:complexType>
            
        </xs:element>

    </xs:schema>

     

    대충 이런 모양이다.

    일단 complexType랑 sequence를 이용한다는 것 기억해두면 좋을 것 같다.

    그리고 나중에 이 .xsd를 사용할 때 note라는 이름의 요소에 링크를 걸어 불러와 사용한다는 것도 알면 좋을 것 같다.

    일단 요소의 데이터타입은 다음과 같이 정의할 수 있다.

    xs:string

    xs:decimal

    xs:integer

    xs:boolean

    xs:date

    xs:time

     

    그리고 다음을 살펴보자

    <xs:element name="lastname" type="xs:string" default="Brown"/>

    <xs:element name="lastname" type="xs:string" fixed="Brown"/>

    default는 요소에 아무값도 넣지 않으면 Brown이 디폴트로 생성되는거고

    fixed는 무조건 Brown만 들어가야한다는거다.

    만약 <lastname>Shown</lastname> 이렇게 쓰면 오류가 난다.

     

    그리고 XML 스키마에서 특정 값에 제한을 거는 방법이 있는데,

    <xs:element name="age">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:minInclusive value="0"/>
                <xs:maxInclusive value="120"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    만약 다음과 같이 코드를 작성하면

    <xs:simpleType>은 간단한 데이터 타입을 정의하겠다는 뜻이다.

    즉, age라는 요소는 하나의 값만 가질 거라는 거다.

    <xs:restriction base="xs:integer">이 부분이 중요하다. 

    xs:integer를 기반으로 한다고 했으니 이 age는 정수형 값으로 제한된다.

    minInclusive와 maxInclusive는 최소값 최대값을 나타낸다.

     

    또 다른 코드를 살펴보자

    <xs:element name="color">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="red"/>
                <xs:enumeration value="green"/>
                <xs:enumeration value="blue"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    Enumeration미리 정해진 값들 중에서 하나만 선택할 수 있게 제한하는 것이다.

    이 예시에서는 color 요소에 red, green, blue 중 하나만 선택할 수 있다는 거다.

     

    또 <xs:whiteSpace value="collapse"/>  이런 것도 있는데

    <example>   Hello      World   </example> 이런 공백이 많은 문장을 

    <example>Hello World</example> 이렇게 하나의 공백으로 처리되도록 해준다.

     

     

    그러면, 한 번 정의한 타입을 여러 번 재사용하려면 어떻게 해야할까?

    예시를 보자

    <xs:element name="employee" type="personinfo"/>
    <xs:complexType name="personinfo">
        <xs:sequence>
            <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    이렇게 complexType에 personinfo라고 타입을 정의해두면 

    employee 요소가 아까 정의한 personinfo 구조를 그대로 가져와 사용할 수 있다.

     

     

    이제 Mixed Content에 대해서 알아보자

    컴플렉스타입에 mixed="ture"를 넣으면

    텍스트와 XML 요소같이 들어갈 수 있다.

    이 예시를 보면

    <letter>
        Dear Mr. <name>John Smith</name>.
        Your order <orderid>1032</orderid>
        will be shipped on <shipdate>2001-07-13</shipdate>.
    </letter>

    텍스트와 하위요소들이 섞여 있는 것을 볼 수 있다.

    이게 바로 혼합 콘텐츠다.

     

     

    '컴공지식 > ' 카테고리의 다른 글

    php에 관하여  (2) 2024.11.15
    노드에 접근하는 법  (0) 2024.10.14
    [XML]DTD와 스키마  (0) 2024.10.12
    PCDATA와 CDATA의 차이  (0) 2024.10.11
    XML DOM  (0) 2024.10.08
Designed by Tistory.