본문 바로가기

Language/C#

XSLT를 이용한 스타일시트 만들기

  1. XSL이란?
    • XML을 사용하고 있는 웹을 통해 전송되는 데이터가 사용자에게 어떻게 보여질 것인지를 나타내는 스타일시트를 만들 때 사용하는 언어
    • XSL의 구성
      1. XSLT : XML 문서의 변환을 위한 언어
        • XML 문서를 HTML문서로 변형 가능
        • XML 문서를 텍스트 형식의 모든 문서 구조로 변형 가능
        • XML 문서를 데이터에스에 입력하기 위한 SQL문으로 변형 가능
      2. XPath : XSL가 XML 문서의 각 부분에 접근하기 위해 필요한 언어
      3. XSLFormation Objects : 포맷팅을 지정하는 XML 표현 형식(어휘) 부분

       

  2. CSS와 XSL 비교
    1. CSS

      XSL

      사용, 이해가 쉽다.

      메모리를 적게 소모

      빠르다.

      XML 문서를 위해 특별히 설계된 언어

      DOM과 같은 프로그래밍 기술을 이용하기 쉽다.

      속성값 표현 가능

      CSS는 XSL를 보완하는 기술로 유용하게 사용된다.

       

  3. MSXML 파서 버전 확인   ==> checkVersion.htm
    •  

  4. XPath
    • XML 문서의 각 부분에 접근할 수 있도록 해 주는 언어
    • <xsl:template match="/">

       

  5. XSL 사용 기본
    • <?xml version="1.0" encoding="euc-kr" ?>

      <?xml-stylesheet type="text/xsl" href="member.xsl"?>

      <member>

           <name>홍길동</name>

           <phone>031-111-2323</phone>

           <mail>test@test.com</mail>

           <address TYPE="회사">경기도 성남시 분당구 ...</address>

           <photo>a01.gif</photo>

           <note>의적</note>

      </member>

    • member.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H2>회원 정보</H2>

              성명: <xsl:value-of select="member/name"/><BR/>

              전화번호: <xsl:value-of select="member/phone"/><BR/>

      </xsl:template>

      </xsl:stylesheet>

    • xhmtl.xml (memeber.xml과 동일)
    • xhtml.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

           <HTML>

              <HEAD>

                 <TITLE> 회원정보보기</TITLE>

              </HEAD>

              <BODY>

                  <H2>회원 정보</H2>

                      성명: <xsl:value-of select="member/name"/><BR/>

                      전화번호: <xsl:value-of select="member/phone"/><BR/>

              </BODY>

            </HTML>

          </xsl:template>

      </xsl:stylesheet>

    • <template> : 새로 만들어지는 트리를 구성하는 각각의 노드에 대한 정의를 하기 위해 사용
    • <template> 요소에서는 원래 문서의 내용을 어떻게 변환할 것인지에 대한 정보가 정의된다.
    • match="/"  : XML 문서 자체에 매칭되는 정보를 지정한다는 것을 의미
    • <BR/> : XSL에서 사용하는 HTML은 기존의 HTML을 XML 형식으로 변환한 XHTML

     

  6. CSS 적용
    • memeber1.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:template match="/">

        <HTML>

         <HEAD>  

              <TITLE>회원 정보</TITLE>

         </HEAD>

         <BODY>

              <H2 STYLE="color:red">회원 정보</H2>

              성명: <SPAN STYLE="font-weight:bold">

                      <xsl:value-of select="member/name"/>             </SPAN><BR/>

              전화번호: <SPAN STYLE="font-weight:bold;font-style:italic">

                            <xsl:value-of select="member/phone"/>     </SPAN><BR/>

         </BODY>

        </HTML>

      </xsl:template>

      </xsl:stylesheet>

    • memeber2.xml (memeber.xml과 동일)
    • memeber2.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:template match="/">

      <HTML>

         <HEAD>  

           <TITLE>회원 정보</TITLE>

           <STYLE>

              H2 { color:red }

              .NAME { font-weight:bold }

              .PHONE { font-weight:bold;font-style:italic }

           </STYLE>

         </HEAD>

         <BODY>

              <H2>회원 정보</H2>

              성명: <SPAN CLASS="NAME">

                           <xsl:value-of select="member/name"/>  </SPAN>  <BR/>

              전화번호: <SPAN CLASS="PHONE">  

                           <xsl:value-of select="member/phone"/> </SPAN> <BR/>

         </BODY>

      </HTML>

      </xsl:template>

      </xsl:stylesheet>

    • 예) family1.xml

     

  7. HTML 적용
    • <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:template match="/">

      <HTML>

         <HEAD>       <TITLE>회원 정보</TITLE>   </HEAD>

         <BODY>

            <center><p/><br/><H2>회원 정보</H2>

              <hr width="60%" color="red" align="center"/>   <br/>

              <font size="5" color="cyan" face="굴림">

                   성명: <xsl:value-of select="member/name"/></font>  <BR/>

              <font size="5" color="blue" face="고딕">

                   전화번호: <xsl:value-of select="member/phone"/></font>  <BR/>

              <font size="5" color="pink" face="명조">

                   메일: <xsl:value-of select="member/mail"/></font>  <BR/>

              <font size="5" color="yellow" face="궁서"><b>

                   주소: <xsl:value-of select="member/address"/></b></font><BR/>  <BR/>

              <hr width="60%" color="red" align="center"/>   <br/>

            </center>

         </BODY>

      </HTML>

      </xsl:template>

      </xsl:stylesheet>

     

  8. 반복하는 내용 표시하기
    • <for-each> 요소를 사용

      <?xml version="1.0" encoding="euc-kr" ?>

      <?xml-stylesheet type="text/xsl" href="member4.xsl"?>

      <members>

         <member>

            <name>홍길동</name>

            <phone>031-111-2323</phone>

            <mail>hong@test.com</mail>

            <address>경기도 성남시 분당구 ...</address>

            <note>의적</note>

         </member>

         <member>

            <name>강감찬</name>

            <phone>031-123-4567</phone>

            <mail>kang@test.com</mail>

            <address>경기도 고양시 덕양구 ...</address>

         </member>

         <member>

            <name>을지문덕</name>

            <phone>02-123-4567</phone>

            <mail>ulji@test.com</mail>

            <address>서울시 중구 을지로3가 ...</address>

         </member>

      </members>

    • memeber4.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H2>회원 정보</H2>

              <xsl:for-each select="members/member">

                      성명: <xsl:value-of select="name"/><BR/>

                      전화번호: <xsl:value-of select="phone"/><BR/>

                       E-mail: <xsl:value-of select="mail"/><P />

              </xsl:for-each>

         </xsl:template>

      </xsl:stylesheet>

     

  9. 다른 템플릿 적용하기
    • <apply-templates> 요소를 사용
    • <apply-templates> 요소는 특정 요소를 위한 템플릿을 따로 만들어 놓고, 상위 템플릿에서 그것을 적용하라는 지시를 하는 것

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H2>회원 정보</H2>

              <xsl:apply-templates select="members/member" />

         </xsl:template>

       

         <xsl:template match="members/member">

                 성명: <xsl:value-of select="name"/><BR/>

                 전화번호: <xsl:value-of select="phone"/><BR/>

                 E-mail: <xsl:value-of select="mail"/><P />

         </xsl:template>

      </xsl:stylesheet>

    • 예) sogae2.xml
    • 예) handphones.xml
    • 예) handphones2.xml (apply-templates 사용)
    • 예) hand5.xml (for-each 사용)

     

      <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books.xsl"?>

      <books>

         <book>

            <author>김홍경</author>

            <title>내 몸은 내가 고친다</title>

            <publisher>넥서스</publisher>

         </book>

         <book>

            <author>김용옥</author>

            <title>철학강의</title>

            <publisher>통나무</publisher>

         </book>

      </books>

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>

              <TABLE  BORDER="1" ALIGN="CENTER">

                 <TR  BGCOLOR="#F0F0F0" >

                       <TH>제목</TH>   <TH>저자</TH>   <TH>출판사</TH>

                 </TR>

                 <xsl:apply-templates select="books/book" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

     

      <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books1.xsl"?>

      <books>

         <domestic>

            <book>

               <author>김홍경</author>

               <title>내 몸은 내가 고친다</title>

               <publisher>넥서스</publisher>

            </book>

            <book>

               <author>김용옥</author>

               <title>철학강의</title>

               <publisher>통나무</publisher>

            </book>

         </domestic>

         <abroad>

            <book>

               <author>J. K. Rowling</author>

               <title>Harry Potter and the Sorcerer's Stone</title>

               <publisher>Bloomsbury Publishing Plc.</publisher>

            </book>

         </abroad>

      </books>

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="80%">

                    <CAPTION>국내서적</CAPTION>

                    <TR  BGCOLOR="#F0F0F0" >

                           <TH>제목</TH> <TH>저자</TH> <TH>출판사</TH>

                     </TR>

                 <xsl:apply-templates select="books/domestic" />

              </TABLE>

              <BR/>

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="80%">

                    <CAPTION>해외서적</CAPTION>

                    <TR  BGCOLOR="#F0F0F0" >

                           <TH>제목</TH> <TH>저자</TH> <TH>출판사</TH>

                     </TR>

                 <xsl:apply-templates select="books/abroad" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

     

     

  10. 이름을 통해 템플릿 부르기
    • <call-template> 요소를 사용
    • <apply-templates>를 쓰는 대신 <call-template>을 사용

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:for-each select="books/book">

                         <xsl:call-template name="book_template" />

                 </xsl:for-each>

              </TABLE>

         </xsl:template>

       

         <xsl:template name="book_template">

                 <TR>

                       <TD><xsl:value-of select="title"/></TD>

                       <TD><xsl:value-of select="author"/></TD>

                       <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

     

  11. XML 문서의 속성값 읽기
    • <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books5.xsl"?>

      <books>

            <book author="김홍경" title="내 몸은 내가 고친다" publisher="넥서스" />

            <book author="김용옥" title="철학강의" publisher="통나무" />

      </books>

    • books5.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

             <H2 ALIGN="CENTER">도서 정보</H2>

             <TABLE BORDER="1"  ALIGN="CENTER">

              <xsl:for-each select="books/book">

                 <TR>

                         <TD><xsl:value-of select="@title"/></TD>

                         <TD><xsl:value-of select="@author"/></TD>

                         <TD><xsl:value-of select="@publisher"/></TD>

                 </TR>

              </xsl:for-each>

             </TABLE>

         </xsl:template>

      </xsl:stylesheet>

       

  12. XHTML 속성값 지정
  • 그림 삽입

                 ==> <IMG>

                            <xsl:attribute name="SRC">

                                     <xsl:value-of select="image"/>

                             </xsl:attribute>

            <xsl:attribute name="width">100</xsl:attribute>

             <xsl:attribute name="height">100</xsl:attribute>

      </IMG>

 

  • 하이퍼링크 연결

              <A>

                         <xsl:attribute name="HREF">

      mailto:<xsl:value-of select="mail"/>

                         </xsl:attribute>

                         <xsl:value-of select="mail"/>

              </A>

 

    <?xml version="1.0" encoding="euc-kr" ?>

    <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

     

       <xsl:template match="/member">

           <br/><br/>

          <TABLE width="60%" border="1" boardcolor="yellow" bgcolor="yellow" align="center">

               <TR> <td border="1" boardcolor="yellow" bgcolor="yellow" align="center">

                  <IMG>

                     <xsl:attribute name="SRC">images/

                               <xsl:value-of select="photo"/>

                     </xsl:attribute>

                     <xsl:attribute name="width">200</xsl:attribute>

                     <xsl:attribute name="height">200</xsl:attribute>

                  </IMG>

               </td> </TR>

               <TR>

                 <td align="center" border="1" boardcolor="yellow" bgcolor="red">

                      성명: <xsl:value-of select="name"/> </td> </TR>

               <TR>

                 <td align="center" border="1" boardcolor="yellow" bgcolor="purple">

                      전화:<xsl:value-of select="phone"/></td></TR>

               <TR>

                 <td align="center" border="1" boardcolor="yellow" bgcolor="yellow">메일:

                  <A>

                     <xsl:attribute name="HREF">mailto:<xsl:value-of select="mail"/>

                     </xsl:attribute>

                     <xsl:value-of select="mail"/>

                  </A> </td>

               </TR>

            </TABLE>

       </xsl:template>

     

    </xsl:stylesheet>

       

  • 프레임 나누기

    결과

    프레임이있는 HTML 소스

    left.htm

    right.htm

    <frameset cols="30%,70%">
          <frame src="left.htm" name="left">
          <frame src="right.htm" name="right">
    </frameset>

    frame.xml

    <?xml version="1.0" encoding="euc-kr" ?>

    <?xml-stylesheet type="text/xsl" href="frame.xsl" ?>

    <!DOCTYPE 프레임 SYSTEM "frame.dtd">

    <프레임>

         <보더>6</보더>

         <비율>25%,75%</비율>

         <왼쪽창주소>menu.xml</왼쪽창주소>

         <왼쪽창이름>menu</왼쪽창이름>

         <오른쪽창주소>body.xml</오른쪽창주소>    

         <오른쪽창이름>body</오른쪽창이름>

    </프레임>

     


    frame.xsl

    <?xml version="1.0" encoding="EUC-KR" standalone="no"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/프레임">

         <FRAMESET>

              <xsl:attribute name="COLS" >

                    <xsl:value-of select="비율"/>

              </xsl:attribute>

              <xsl:attribute name="BORDER" >

                    <xsl:value-of select="보더"/>

              </xsl:attribute>

              <FRAME>

                    <xsl:attribute name="SRC" >

                        <xsl:value-of select="왼쪽창주소"/>

                    </xsl:attribute>

                    <xsl:attribute name="NAME" >

                        <xsl:value-of select="왼쪽창이름"/>

                    </xsl:attribute>

              </FRAME>

              <FRAME>

                    <xsl:attribute name="SRC" >

                        <xsl:value-of select="오른쪽창주소"/>

                    </xsl:attribute>

                    <xsl:attribute name="NAME" >

                        <xsl:value-of select="오른쪽창이름"/>

                    </xsl:attribute>

              </FRAME>

         </FRAMESET>

    </xsl:template>

    </xsl:stylesheet>

    top.htm

    bottom.htm

    <frameset rows="50%,50%">
          <frame src="top.htm" name="top">
          <frame src="bottom.htm" name="bottom">
    </frameset>

    top.htm

    middle.htm

    bottom.htm

    <frameset rows="20%,30%,50%">
          <frame src="top.htm" name="top">
          <frame src="middle.htm" name="middle">
          <frame src="bottom.htm" name="bottom">
    </frameset>

    top.htm

    mid
    dle
    .htm

    bottom.htm

    <frameset rows="20%,80%">
          <frame src="top.htm" name="top">

             <frameset cols="30%,70%">
                <frame src="middle.htm" name="middle">
                <frame src="bottom.htm" name="bottom">

             </frameset>
    </frameset>

    top.htm

    left
    .htm

    right.htm

    bottom.htm

     

    프레임1

    프레임2

    <frameset rows="20%,60%,20%">
          <frame src="top.htm" name="top">

              <frameset cols="30%,70%">
                  <frame src="left.htm" name="left">
                  <frame src="right.htm" name="right">
             </frameset>

          <frame src="bottom.htm" name="bottom">
    </frameset>

    1

    2

    3

    4

    <frameset cols="50%,50%" rows="50%,50%">
          <frame src="1.htm" name="1">
          <frame src="2.htm" name="2">
          <frame src="3.htm" name="3">
          <frame src="4.htm" name="4">
    </frameset>

    <?xml version="1.0" encoding="euc-kr" ?>

    <?xml-stylesheet type="text/xsl" href="frame.xsl" ?>

    <프레임>

      <비율1> 20%, 60%, 20% </비율1>

      <비율2> 20%, 80%         </비율2>

      <위>

        <화면 속성="위">

          <이름> top </이름>

          <소스> top.xml </소스>

        </화면>

      </위>

      <아래>

        <화면 속성="아래">

          <이름> top </이름>

          <소스> top.xml </소스>

        </화면>

      </아래>

      <가운데>

        <화면 속성="왼쪽">

          <이름> top </이름>

          <소스> top.xml </소스>

        </화면>

        <화면 속성="오른쪽">

          <이름> top </이름>

          <소스> top.xml </소스>

        </화면>

      </가운데>

    </프레임>

    <?xml version="1.0" encoding="EUC-KR" standalone="no"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/프레임">

      <FRAMESET>

        <xsl:attribute name="ROWS" >

          <xsl:value-of select="비율1"/>

        </xsl:attribute>

        <xsl:attribute name="BORDER" > 3 </xsl:attribute>

        <xsl:apply-templates select="위/화면" />

         

        <FRAMESET>

        <xsl:attribute name="COLS" >

          <xsl:value-of select="비율2"/>

        </xsl:attribute>

        <xsl:apply-templates select="가운데/화면" />

        </FRAMESET>

         

        <xsl:apply-templates select="아래/화면" />

      </FRAMESET>

      </xsl:template>

       

       

      <xsl:template match="화면">

        <FRAME>

          <xsl:attribute name="SRC" >

            <xsl:value-of select="소스"/>

          </xsl:attribute>

          <xsl:attribute name="NAME" >

            <xsl:value-of select="이름"/>

          </xsl:attribute>

        </FRAME>

      </xsl:template>

    </xsl:stylesheet>

    <?xml version="1.0" encoding="euc-kr" ?>

    <?xml-stylesheet type="text/xsl" href="frame1.xsl" ?>

    <프레임>

      <비율1> 20%, 60%, 20% </비율1>

      <비율2> 20%, 80% </비율2>

      <화면 속성="위">

        <이름> top </이름>

        <소스> top.xml </소스>

      </화면>

      <화면 속성="아래">

        <이름> top </이름>

        <소스> top.xml </소스>

      </화면>

      <화면 속성="왼쪽">

        <이름> top </이름>

        <소스> top.xml </소스>

      </화면>

      <화면 속성="오른쪽">

        <이름> top </이름>

        <소스> top.xml </소스>

      </화면>

    </프레임>

    <?xml version="1.0" encoding="EUC-KR" standalone="no"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/프레임">

      <FRAMESET>

        <xsl:attribute name="ROWS" >

          <xsl:value-of select="비율1"/>

        </xsl:attribute>

        <xsl:attribute name="BORDER" > 3 </xsl:attribute>

        <xsl:apply-templates select="화면[1]" />

        <FRAMESET>

          <xsl:attribute name="COLS" >

            <xsl:value-of select="비율2"/>

          </xsl:attribute>

          <xsl:apply-templates select="화면[3]" />

          <xsl:apply-templates select="화면[4]" />

        </FRAMESET>

        <xsl:apply-templates select="화면[2]" />

      </FRAMESET>

    </xsl:template>

     

    <xsl:template match="화면">

      <FRAME>

        <xsl:attribute name="SRC" >

          <xsl:value-of select="소스"/>

        </xsl:attribute>

        <xsl:attribute name="NAME" >

          <xsl:value-of select="이름"/>

        </xsl:attribute>

      </FRAME>

    </xsl:template>

    </xsl:stylesheet>

  • 스크립트 삽입하기
  • <?xml version="1.0" encoding="EUC-KR"?>

    <?xml-stylesheet type="text/xsl" href="body.xsl"?>

    <매뉴얼>

    <내용>

            <장>

            <중제목>그 많던 싱아는 누가다 먹었는가</중제목>

            <소제목>작가:박완서 출판일:2002년 8월 인기도:☆☆☆☆☆ 가격:9000원</소제목>

            <본문>평온했던 어린시절에서 전쟁을 치르고 분단이 된 민족사 안에서의 자신과

              가족들에 대한 이야기를 탁월한 문체로 잔잔하게 읊고 있다. 시큼한 싱아에

              물든 고향이 아련하게 다가오는 듯 하다.</본문>

            </장>

            <장>

            <중제목>칼리 피오리나</중제목>

            <소제목>작가:조지앤더스 출판일:2002년 9월 인기도:☆☆☆ 가격:9000원</소제목>

            <본문>2003년 비즈니스위크`올해의 인물’ 등 매년 최고의 수식어로 한해를 시작하는

                 CEO 칼리 피오리나는 전통기업인 휴렛팩커드(HP)를 세계적인 IT 기업으로 전환

                 재구성한 기업 경영 스토리다.</본문>

           </장>

    </내용>

    </매뉴얼>

    <?xml version="1.0" encoding="EUC-KR" ?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">

    <html lang="kr">

    <head>

    <title>전자책</title>

    <style type="text/css">

    .frame1{position:absolute;font-size:11pt;border-width:thick;border-color:gray;

       border-style:double;height:120;width:360;overflow:auto;padding:3;margin-top:20;}

    .frame2{position:absolute;font-size:11pt;border-width:thick;border-color:gray;

       border-style:double;height:80;width:360;overflow:auto;padding:3;margin-top:140;}

    .frame3{position:absolute;font-size:13pt;border-width:thick;border-color:gray;border-style:

       double;height:200;width:470;overflow:auto;padding:3;margin-left:390;margin-top:20;}

    </style>

     

    <script type="text/vbscript">

    <xsl:comment>

    <![CDATA[

    option explicit

    dim xmldoc,i,allNo

    dim headerNode,subject

    dim headerName

    sub window_onload()

        'XML DOM 오브젝트를 사용하도록 한다.(약속)

        set xmldoc=createObject("Microsoft.XMLDom")

        xmldoc.async=false

        xmldoc.load("body.xml")

        

        '"중제목"의 내용에 액세스한다.

        set headerNode=xmldoc.getElementsByTagName("중제목")

     

        '"중제목"이 있으면 그 내용을 변수 subject에 저장한다.

        if headerNode.length>0 then

            for i=0 to headerNode.length-1

                subject=subject & "<div style='cursor:hand' id='headerID" & i & "'" & " onclick='headerGo(" & i & ")'>" & headerNode(i).text & "</div>"

            next

        else

            exit sub

        end if

            '중제목을 나타낸다.

            headerMenu.innerHTML=subject

    end sub

     

    sub headerGo(allNo)

        '중제목을 클릭했을 때의 배경색

        for i=0 to headerNode.length-1

            document.all("headerID" & i).style.backgroundColor="white"

        next

            document.all("headerID" & allNo).style.backgroundColor="silver"

     

            '클릭한 중제목을 변수 headerName에 저장한다.

            headerName=document.all("headerID" & allNo).innerText

            call headerSearch(headerName)

    end sub

     

    sub headerSearch(headerName)

        dim patternstring,body0String

        dim selNode,subject0,body0Node,body0Letter

        header2Menu.innerHTML=""

        body0.innerHTML=""

     

        '중제목을 저장한 변수 headerName를 검색 키로 각각의 요소 내용을 검색한다.

        body0String="매뉴얼/내용/장[중제목='" & headerName & "'" & "]/본문"

        set body0Node=xmldoc.selectNodes(body0String)

     

        patternstring="매뉴얼/내용/장[중제목='" & headerName & "'" & "]/소제목"

        set selNode=xmldoc.selectNodes(patternstring)

     

        if body0Node.length>0 then

            body0Letter=body0Node(0).text

        else

            exit sub

        end if

     

        '소제목이 없는 경우에는 메시지를 나타낸다.

        if selNode.length>0 then

            subject0=selNode(0).text

        else

            subject0="<b style='color:red'>" & "이 장에는 소제목이 없습니다." & "</b>"

        end if

        

            body0.innerHTML=body0Letter

            header2Menu.innerHTML=subject0

    end sub

     

    ]]>

    </xsl:comment>

    </script>

    </head>

    <body>

      <h2 style="color:red">전차책</h2>

      <b style="position:absolute;color:blue">∇책제목을 클릭해주세요..^^</b>

      <div class="frame1" id="headerMenu"></div>

      <div class="frame2" id="header2Menu">책에 대한 정보를 보여줍니다. </div>

      <div class="frame3" id="body0">본문 내용이 나타납니다.</div>

    </body>

    </html>

    </xsl:template>

    </xsl:stylesheet>

 

  1. 테이블에 번호 매기기
    • <xsl:number format="format 속성값">
    • format 속성값

      표시형식

      1

      1,2,3....

      01

      01,02,03......

      a

      a,b,c....

      A

      A,B,C.....

      i(알파벳 소문자 i)

      i,ii,iii....

      I(알파벳 대문자 I)

      I,II,III......

     

  2. 데이터 정렬
    • <sort> 요소를 사용

      <xsl:srot

             select = 요소이름

             data-type = { "text"  | "number" }

             order = { "ascending"  | "descending" }

             case-order = { "upper-first"  | "low-first" }  / >

    •  books7.xml   (publisher요소의 문자 데이터 기준으로 정렬)

      <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books7.xsl"?>

      <books>

         <book>

              <author>김홍경</author>

              <title>내 몸은 내가 고친다</title>

              <publisher>넥서스</publisher>

              <price>10000</price>

              <pub_date>2000-09-05</pub_date>

         </book>

         <book>

              <author>김용옥</author>

              <title>철학강의</title>

              <publisher>통나무</publisher>

              <price>7500</price>

              <pub_date>1990-03-01</pub_date>

         </book>

         <book>

              <author>데이비드 보더니스</author>

              <title>E=mc2</title>

              <publisher>생각의나무</publisher>

              <price>13000</price>

              <pub_date>2001-03-27</pub_date>

         </book>

      </books>

    • boo7.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">출판사 기준으로 정렬된 도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <TR  BGCOLOR="#F0F0F0" >

                       <TH>제목</TH>                 <TH>저자</TH>

                       <TH>출판사</TH>              <TH>가격</TH>

                 </TR>

                 <xsl:apply-templates select="books/book">

                      <xsl:sort select="publisher"/>

                 </xsl:apply-templates>

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                    <TD align="right"><xsl:value-of select="price"/></TD>

                 </TR>

         </xsl:template>

       

      </xsl:stylesheet>

    • books7-1.xml   (publisher요소의 문자 데이터 기준으로 정렬)

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

       

         <xsl:template match="/">

              <H3 ALIGN="CENTER">출판사 기준으로 정렬된 도서 정보</H3>

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <TR  BGCOLOR="#F0F0F0" >

                       <TH>제목</TH>           <TH>저자</TH>

                       <TH>출판사</TH>        <TH>가격</TH>

                 </TR>

                 <xsl:apply-templates select="books/book">

                      <xsl:sort select="price" data-type="number"

                                                               order="descending" />

                 </xsl:apply-templates>

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                    <TD align="right"><xsl:value-of select="price"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

         

    • phones3.xml  

             (통신사를 1순위로 가격 현금일시불을 기준으로 2순위로 오름차순으로 정렬)

        <xsl:sort order="ascending" select="통신사" />

        <xsl:sort order="ascending" select="가격/현금일시불" />

    • phones4.xml  

            (통신사를 1순위로 가격 현금일시불을 기준으로 2순위로 오름차순으로 정렬)

      <xsl:sort order="ascending" select="통신사"/>

      <xsl:sort order="ascending" select="가격/현금일시불" data-type="number"/>

      <xsl:value-of select="format-number(가격/현금일시불, '###,###,##0')" />

         

      ※ format-number(태그, 포맷 형식)

        ☞ # : 해당 자리에 숫자값이 있으면 나타내고 값이 없으면 나타내지 말라는 의미

        ☞ 0 : 해당 자리에 숫자값이 있으면 나타내고 값이 없으면 숫자 0을 나타내라는 의미 

         

    • books6-1.xml

      <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books6-1.xsl"?>

      <books>

         <book>

            <author>김홍경</author>

            <title>내 몸은 내가 고친다</title>

            <publisher>넥서스</publisher>

         </book>

         <book>

            <author>김용옥</author>

            <title>철학강의</title>

            <publisher>통나무</publisher>

            <not-for-sale/>

         </book>

      </books>

     

  3. 동일한 위계에 있는 요소 중 특정한 위치에 있는 요소의 필터링
    • ==> 요소 이름 다음에 대괄호를 쓰고 그 안에 자식 요소의 위치를 쓰면 해당 위치의 자식 요소만 추출된다.

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3> 

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="books/book[1]" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

     

  4. 특정한 이름의 자식 요소를 가진 요소의 필터링
  5.                  요소 이름 다음에 대괄호를 쓰고 그 안에 자식 요소를 가진 요소 이름을 쓰면 추출된다.

      • 주의할 점)
      •  대괄호를 쓰기 전에 나타나는 요소 이름이 <apply-templates>이 가리키는 요소이다.

         <apply-templates>의 적용을 받는 것은 <xsl:template match="book"> 요소이다.

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">비매품 도서 정보</H3> 

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="books/book[not-for-sale]" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                       <TD><xsl:value-of select="title"/></TD>

                       <TD><xsl:value-of select="author"/></TD>

                       <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

     

  6. 문자 데이터나 속성값 필터링
    • XPath의 논리 및 비교 연산자

      연산자

      의미

      연산자

      의미

      and

      논리합

         [author = '김홍경' and title = '철학강의]

         [author = '김홍경'] [title = '철학강의]

      or

      논리곱

      &lt;

      작다(<)

      not()

      논리부정

      &lt;=

      작거나 같다.(<=)

      =

      같다

      &gt;

      크다(>)

      !=

      다르다

      &gt;=

      크거나 같다(>=)

    •  XPath의 산술 연산자
      • +, - *, div(나눗셈), mod(나눈 나머지)

     

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">비매품 도서 정보</H3>

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="books/book[author = '김홍경']" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

      </xsl:stylesheet>

    • <xsl:apply-templates select="books/book[author != '김홍경']" />
    • phones6.xml  (현금일시불 가격이 150,000원 미만인 핸드폰 리스트만을 출력)

      <xsl:apply-templates select = "/제품/핸드폰[가격/현금일시불[@화폐='원'] &lt; 150000]">

     

  7. 문자 데이터나 속성값의 패턴 매칭
    • <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">비매품 도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="books/book[contains(title, '철학')]" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

       

      </xsl:stylesheet>

     

  8. 노드를 나타내는 XPath 연산자
    • 연산자

      설명

      용례

      /

      자식 노드를 지정

      books/book   ⇒ <books>의 자식요소 <book>

      //

      후손 전체를 지정

      books//title ⇒ <books> 요소의 후손 중 title 요소 전체

      *

      와일드 카드

      book/* ⇒ <book> 요소의 자식 요소 전체

      book/@* ⇒ <book> 요소의 속성 전체

      .

      자기 자신

      .//title

      ..

      부모 노드

      ../title ⇒ 형제 요소인 title

      |

      노드의 논리합

      books/book/domestic | books/book/abroad

         ⇒ books/book/domestic과 books/book/abroad을 모두 찾는다.

    • books8.xml

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">통나무 출판사 도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                      <TR>

                             <TH>제목</TH>               <TH>저자</TH>

                      </TR>

                      <xsl:for-each select="//title[../publisher = '통나무']">

                         <TR>

                             <TD><xsl:value-of select="."/></TD>

                             <TD><xsl:value-of select="../author"/></TD>

                         </TR>

                      </xsl:for-each>

              </TABLE>

      </xsl:template>

      </xsl:stylesheet>

       

  9. XSLT에서의 조건 처리
    • <if>요소

      <if test = "조건식">

                    조건식이 참일 때의 처리 내용

      </if>

      <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books10.xsl"?>

      <books>

         <book>

            <author>김홍경</author>

            <title>내 몸은 내가 고친다</title>

            <publisher>넥서스</publisher>

            <price>10000</price>

            <pub_date>2000-09-05</pub_date>

         </book>

         <book>

            <author>김용옥</author>

            <title>철학강의</title>

            <publisher>통나무</publisher>

            <price>7500</price>

            <pub_date>1990-03-01</pub_date>

         </book>

         <book>

            <author>데이비드 보더니스</author>

            <title>E=mc2</title>

            <publisher>생각의나무</publisher>

            <price>13000</price>

            <pub_date>2001-03-27</pub_date>

         </book>

      </books>

      • books10.xsl

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/books">

              <TABLE  border="1" align="center" width="90%">

                      <xsl:apply-templates select="book" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD>

                             <xsl:if test="substring(pub_date, 1, 4) = '2001'">

                                      <IMG src="images\new.gif"/>&#x20;

                         </xsl:if>      

                             <xsl:value-of select="title"/>

                    </TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

       

      </xsl:stylesheet>

    • hand2.xml  (색상태그의 내용이 있으면 그 내용을 출력하고 내용이 없으면 -를 출력)

      <xsl:if test="count(색상) = 0">   -   </xsl:if>

      <xsl:if test="count(색상) != 0">  <xsl:value-of select="색상" />   </xsl:if>

         

    • <choose>

      <choose>

              <when  test = "조건식">

                    조건식이 참일 때의 처리 내용

               </when>

                <otherwise>

                      위의 모든 조건이 거짓일 때의 처리 내용

                </otherwise>

      </choose>

      • books10-1.xsl

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/books">

              <TABLE  border="1" align="center" width="90%">

                      <xsl:apply-templates select="book" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <xsl:choose>

                        <xsl:when test="price &lt; 10000">

                           <TD style="color:#000000">

         <xsl:value-of select="price"/></TD>

                        </xsl:when>

                        <xsl:when test="price &lt; 20000">

                           <TD style="color:#770000">

        <xsl:value-of select="price"/></TD>

                        </xsl:when>

                        <xsl:otherwise>

                           <TD style="color:#ff0000">

        <xsl:value-of select="price"/></TD>

                        </xsl:otherwise>

                    </xsl:choose>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

       

      </xsl:stylesheet>

    • hand3.xml  (현금일시불 태그가 존재하면 현금일시불의 값을 나타냄,  카드일시불이면 카드일시불의 값을 나타내고, 할부엘리먼트가 존재하면 할부엘리먼트의 값에다가 할부엘리먼트의 개월 속성값을 곱해서 출력)

      <xsl:choose>

             <xsl:when test="count(가격/현금일시불) != 0">

               <xsl:value-of select="format-number(가격/현금일시불, '###,###,##0')" />

             </xsl:when>

             <xsl:when test="count(가격/카드일시불) != 0">

               <xsl:value-of select="format-number(가격/카드일시불, '###,###,##0')" />

             </xsl:when>

             <xsl:when test="count(가격/할부) != 0">

                  <xsl:value-of select="format-number(가격/할부 * 가격/할부/@개월,

                                            '###,###,##0')" />

             </xsl:when>

             <xsl:otherwise>가격정보 없음</xsl:otherwise>

        </xsl:choose>

     

  10. XPath 함수
  • 노드셋 함수
    • <for-each>나 <apply-templates> 요소의 select 속성값으로 매칭 될 수 있는 1개 이상의 노드 집합
    • last()  :   현재 노드가 들어 있는 노드셋에서 전체 멤버 노드의 갯수
    • position()  :   현재 노드의 위치
    • count(node-set)   : 노드셋을 인수로 받아 그 안의 노드 개수
    • sum(node-set)   :   노드셋에 있는 모든 숫자의 합을 반환
    • id(object)   :   속성 유형이 id인 속성의 값을 인수로 받아 해당 id를 가진 요소를 반환
    • local-name(node-set?)   :   현재 노드의 이름을 반환
      • 이름공간 접두어가 있을 경우 이름 공간 접두어가 제거된 값이 반환
    • namespace-uri(node-set?)   :   현재 노드의 이름 공간 URI를 반환
    • name(node-set?)   :   현재 노드의 이름을 반환
    • member5.xml
    • <?xml version="1.0" encoding="euc-kr"?>

      <!DOCTYPE members

        [

               <!ELEMENT members (member+)>

               <!ELEMENT member (name, phone_no)>

                        <!ATTLIST member

                                    no              ID      #REQUIRED

                                    parent-no       IDREFS  #IMPLIED>

                        <!ELEMENT name (#PCDATA)>

                        <!ELEMENT phone_no (#PCDATA)>

        ]>

       

      <?xml-stylesheet type="text/xsl" href="member5.xsl"?>

      <members>

          <member no="A01" parent-no= "A02 A03">

              <name>이이</name>

              <phone_no>031-111-2323</phone_no>

        </member>

        <member no="A02">

              <name>신사임당</name>

              <phone_no>031-333-2323</phone_no>

        </member>

        <member no="A03">

              <name>이원수</name>

              <phone_no>02-123-3456</phone_no>

        </member>

      </members>

      • member5.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

             <H2 align="center">회원 정보</H2>      

             <TABLE border="1"  align="center" width="80%">

                 <TR>

                    <TH>NO.</TH>            <TH>회원번호</TH>

                    <TH>성명</TH>           <TH>전화번호</TH>

                 </TR>

              <xsl:for-each select="members/member">

                 <TR>

                    <TD align="right"><xsl:value-of select="position()"/>

                          /    <xsl:value-of select="last()"/></TD>

                    <TD align="right"><xsl:value-of select="@no"/></TD>

                    <TD align="right"><xsl:value-of select="id(@no)/name"/></TD>

                    <TD align="right"><xsl:value-of select="phone_no"/></TD>

                 </TR>

              </xsl:for-each> 

                 <TR>

                  <TD></TD><TD></TD><TD></TD>

                  <TD align="right">총 <xsl:value-of select="count(members/member)"/>명

                  </TD>

                 </TR>

             </TABLE>

         </xsl:template>

       

      </xsl:stylesheet>

      • hand4.xml  (전체 항목의 개수와 합계 구해서 출력)

 

  • 문자열 함수
    • string(object)
    • concat(문자, 문자, 문자*)
      • concat('ab', 'cd', 'ef', 'gh')  ==> abcdefgh
    • starts-with(문자, 문자)
      • starts-with('홍길동', '홍')  ==> true
    • contains(문자, 문자)
      • contains('홍길동', '동')  ==> true
    • substring-before(문자, 문자)
      • substring-before('2001/6/30' , '/')  ==> 2001
    • substring-after(문자, 문자)
      • substring-after('2001/6/30' , '/')  ==> 6/30
    • substring(문자, 숫자, 숫자)
      • substring('2001-06-30' , 6 , 2)  ==> 06
    • string-length(문자?)
      • string-length('홍길동 멋쟁이!')  ==> 8
    • normalize-space(문자?)
      • string-length( normalize-space('test   ok') )  ==> 6
    • translate(문자, 문자, 문자)
      • translate('2001/06/30' , '/' , '-')  ==> 2001-06-30

       

  • 부울린 함수
    • boolean(object)   :   인수를 true/false로 변환한 값을 반환
    • not(boolean)   :   논리부정
    • true()   :   true로 반환
    • false()   :   false로 반환
    • lang(문자)   : 노드에서 인수값이 xml:lang 속성의 값과 같은 것을 찾아 반환
    • books9.xml
    • <?xml version="1.0" encoding="euc-kr"?>

      <?xml-stylesheet type="text/xsl" href="books9.xsl"?>

      <books>

            <book>

               <author xml:lang="kr">조앤.K.롤링</author>

               <author xml:lang="en">J. K. Rowling</author>

               <title xml:lang="kr">해리포터와 마법사의 돌</title>

               <title xml:lang="en">Harry Potter and the Sorcerer's Stone</title>

               <publisher xml:lang="kr">문학수첩</publisher>

               <publisher xml:lang="en">Bloomsbury Publishing Plc.</publisher>

            </book>

      </books>

    • books9.xsl
    • <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER">

                 <xsl:for-each select="books/book">

                 <TR>

                       <TD><xsl:value-of select="title[lang('kr')]"/><BR/>

                                (<xsl:value-of select="title[lang('en')]"/>)</TD>

                       <TD><xsl:value-of select="author[lang('kr')]"/></TD>

                       <TD><xsl:value-of select="publisher[lang('kr')]"/></TD>

                 </TR>

                 </xsl:for-each>

              </TABLE>

         </xsl:template>

       

      </xsl:stylesheet>

 

  • 숫자 관련 함수
    • number(object?)   :   인수를 숫자로 변환해 반환  (숫자로 변환될 수 없으면 'NaN'을 반환)
    • sum(node-set)   :   노드셋에 있는 모든 숫자의 합을 반환
    • floor(숫자)   :   인수로 받은 숫자보다 크지 않은 정수 중 최대값을 반환
    • ceiling(숫자)   :   인수로 받은 숫자보다 작지 않은 정수 중 최소값을 반환
    • round(숫자)   :   반올림한 결과를 반환
    • format-number(숫자, 형식)
      • format-number(12345, '#,###')   ==> 12,345
      • format-number(12, '0000')   ==> 0012

     

  1. 변수선언
    • 변수선언 형식
      • <xsl:variable name="변수명" select="변수값"/>
      • <xsl:variable name="변수명">변수값</xsl:variable>
    • select 속성값 지정시 주의점
      • 변수값이 문자열인 경우는 반드시 ' '로 감싸야 한다.
      • ' '로 감싸지 않으면 노드명으로 인식하고 노드의 컨텐트 내용을 값으로 가짐
    • 변수값이 단순한 문자열이고, 엘리먼트의 컨텐트 내용으로 사용되는 경우
      • <엘리먼트> <xsl:value-of select="$변수명"/</엘리먼트>
    • 변수값이 단순한 문자열이고, 엘리먼트의 속성값으로 사용되는 경우
      • 속성명 = "{$변수명}"
    • 변수값이 단순한 문자열이고, 엘리먼트의 속성값으로 사용되는 경우
      • 속성명 = "{$변수명}"
    • 변수값이 단순한 문자열이고, XPath 표현식에서 사용되는 경우
      • 노드명=$변수명   또는   @속성명 = $변수명
    • 엘리먼트를 포함하고 있는 변수 사용되는 경우
      • <엘리먼트>  <xsl:copy-of select = "$변수명"/>  </엘리먼트>
    • 예) 변수.xml   변수.xsl

 

    • <xsl:param> : 값을 문자 데이터로 저장
    • books11.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

       

         <xsl:param name="filterTitle">철학강의</xsl:param>

         <xsl:variable name="filterSyntax" select="books/book[title = $filterTitle]" />

       

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="$filterSyntax" />

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                 <TR>

                    <TD><xsl:value-of select="title"/></TD>

                    <TD><xsl:value-of select="author"/></TD>

                    <TD><xsl:value-of select="publisher"/></TD>

                 </TR>

         </xsl:template>

       

      </xsl:stylesheet>

    • books11-1.xsl

      <?xml version="1.0" encoding="euc-kr" ?>

      <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

       

         <xsl:template match="/">

              <H3 ALIGN="CENTER">도서 정보</H3>  

              <TABLE  BORDER="1" ALIGN="CENTER" WIDTH="90%">

                 <xsl:apply-templates select="books/book" >

                      <xsl:with-param name="prmPrice" select="8000"/>

                 </xsl:apply-templates>

              </TABLE>

         </xsl:template>

       

         <xsl:template match="book">

                   <xsl:param name="prmPrice"/>

                <xsl:if test="price &gt; $prmPrice">

                     <TR>

                        <TD><xsl:value-of select="title"/></TD>

                        <TD><xsl:value-of select="author"/></TD>

                        <TD><xsl:value-of select="publisher"/></TD>

                     </TR>

                 </xsl:if>

         </xsl:template>

       

      </xsl:stylesheet>

       

    • 스크립트 만들기
      • <SCRIPT> 태그 안에 내용은 CDATA 섹션에 둔다.
      • MSXML 파서가 'http://www.w3.org/TR/WD-xsl'을 이름공간으로 사용하던 때에는 <script>를 요소를 사용했다.
      • 그러나 xslt 1.0 권고안에서는 <script> 요소가 사라졌다.

       

    • 스타일시트를 처리하는 파서가 사용하는 스크립트
      • <?xml version="1.0" encoding="euc-kr" ?>

        <?xml-stylesheet type="text/xsl" href="member6.xsl"?>

        <members>

           <member>

                <name>홍길동</name>

                <phone>031-111-2323</phone>

                <mail>hong@test.com</mail>

                <address>경기도 성남시 분당구 ...</address>

                <note>의적</note>

           </member>

           <member>

                <name>강감찬</name>

                <phone>031-123-4567</phone>

                <mail>kang@test.com</mail>

                <address>경기도 고양시 덕양구 ...</address>

           </member>

           <member>

                <name>을지문덕</name>

                <phone>02-123-4567</phone>

                <mail>ulji@test.com</mail>

                <address>서울시 중구 을지로3가 ...</address>

           </member>

        </members>

      • member6.xsl

        <?xml version="1.0" encoding="euc-kr"?>

        <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"

                xmlns:msxsl="urn:schemas-microsoft-com:xslt"

                xmlns:user="urn:user-namespace-here" >

         

        <xsl:template match="/members">

              <H1 align="center">서울/경기 지역 회원 리스트</H1>   

              <H3 align="center">서울 지역 회원</H3>

              <TABLE border="1" align="center" width="90%">

                    <xsl:apply-templates select=

                "member[substring(address, 1, 2) = '서울']"/>

              </TABLE>

              <BR/><BR/>

              <H3 align="center">경기도 지역 회원</H3>

              <TABLE border="1" align="center" width="90%">

                       <xsl:apply-templates select=

                                   "member[substring(address, 1, 2) = '경기']"/>

              </TABLE>

        </xsl:template>

         

        <xsl:template match="member">

                <xsl:if test="position() = 1"> <!-- 처음일 경우 제목 줄을 만든다 -->

                     <TR>

                         <TH>NO</TH> <TH>성명</TH> <TH>전화번호</TH> <TH>주소</TH>

                     </TR>

                </xsl:if>

                <TR>

                     <TD width="10%" align="right">

                  <xsl:value-of select="position()"/>/

                  <xsl:value-of select="user:getSerialNo()"/></TD>

                       <TD width="15%"><xsl:value-of select="name"/></TD>

                       <TD width="23%"><xsl:value-of select="phone"/></TD>

                       <TD width="52%"><xsl:value-of select="address"/></TD>

                </TR>

        </xsl:template>

         

        <msxsl:script language="JScript" implements-prefix="user">

              <![CDATA[

                     var count = 0;

                     function getSerialNo()

                          { return(++count); }

               ]]>

        </msxsl:script>

        </xsl:stylesheet>

       

    • 실습문제
      • dayran.xml  
      • dayran.xsl
      • <?xml version="1.0" encoding="euc-kr"?>

        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

        <xsl:template match="/">

            <SCRIPT><![CDATA[

               function changeMode (mode) {

                   if (mode == 1) { // 자세히

                        divBrief.style.display = "none";

                        divDetail.style.display = "inline";

                   }

                   else { // 간략히

                        divBrief.style.display ="inline";

                        divDetail.style.display = "none";

                   }

                }

             ]]></SCRIPT>

         

          <H2 align="center"><font color="hotpink"> 14일의 의미</font> </H2>

         

          <DIV id="divBrief">

          <TABLE align="center" width="90%">

             <TR align="right">

                 <TD/><TD/>

                 <TD><INPUT id="btnChangeMode" type="button"

                               value="자세히" onClick="changeMode(1)"/></TD>

             </TR>

             <TR bgcolor="#f0f0f0">

                 <TH width="20%"><font color="magenta">월/일</font></TH>

                 <TH width="80%"><font color="magenta">데이 의미 / 내용</font></TH>         

              </TR>

              <xsl:for-each select="뭔데이/데이">

                 <TR>

                     <TD align="center"><font color="violet">

                                   <xsl:value-of select="@월일"/></font></TD>

                     <TD><font color="violet"><xsl:value-of select="@의미"/></font></TD>

                 </TR>

               </xsl:for-each>

          </TABLE>

          </DIV>

         

          <DIV id="divDetail" style="display:none">

          <TABLE align="center" width="90%">

              <TR align="right">

                  <TD/><TD/>

                  <TD><INPUT id="btnChangeMode" type="button" value="간략히"

                                onClick="changeMode(0)"/></TD>

              </TR>

              <TR bgcolor="f0f0f0">

                 <TH width="20%"><font color="magenta">월/일</font></TH>

                 <TH width="80%"><font color="magenta">데이 의미 / 내용</font></TH>

              </TR>

              <xsl:for-each select="뭔데이/데이">

                 <TR>

                     <TD align="center"><font color="magenta">

                              <xsl:value-of select="@월일"/></font></TD>

                     <TD><font color="magenta"><xsl:value-of select="@의미"/></font></TD>

                 </TR>

                             <xsl:for-each select="내용">

                    <TR><TD/><TD style="font-size=.7em"><font color="#00b0ff">

                            <xsl:value-of select="."/></font></TD>

                    </TR>

                 </xsl:for-each>

               </xsl:for-each>

             </TABLE>

             </DIV>

           </xsl:template>

        </xsl:stylesheet>

    출처: http://xmljang.com.ne.kr/content/ch6-xsl.html