달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

# XSLT (eXtensible Stylesheet Language Transformations)
  XSL(XML Stylesheet Language)
   : CSS의 기능과 유사하게 XML 문서에 대한 style을 정의하고 적용할 때 사용하는 언어
  XSLT
   : XSL 표준을 구성하는 일부, XML 데이터를 변경할 때 필요한 '규칙'을 정의할 목적으로 고안됨.
   : 주로 XML 데이터를 HTML 페이지로 변환하여 이를 브라우저에 출력하는데 이용

# XSL, XSLT, XSL-FO
  XSLT : 문서의 변환 규칙을 정의하기 위한 언어
  XSL-FO : XSL, 즉 XML을 위한 stylesheet 정의 언어에서 이용될 formatting 객체를 정의하는 언어. 문서가 어떻게 꾸며져야 하는지 지정하기 위해 사용.

#. XSLT의 기본 구조

1. stylesheet 문서
  XSLT 문서는 transform element를 root로 하며, XSL에 대한 namespace와 하나 이상의 template 정보를 포함한다.

  <?xml version="1.0"?>
  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
   <head><title>Thank you letter</title></head>
   <body>
   ...
   </body>
 </html>
  </xsl:template>
  </xsl:transform>

2. 값의 출력
  XSLT의 value-of element를 이용하여 프로세서를 통해 특정 정보 아이템을 출력할 수 있다.

# XPath의 개요

1. 자식과 후손 엘리먼트
  XPath는 XML 문서 내에 현재 방문중인 노드(context node)에서 이동하고자 하는 노드에 대한 위치 정보를 제공
  context node가 결정되면 다른 node로의 방문은 file system 이나 URL과 유사하다.

 <?xml version="1.0"?>
 <content>
  <events>
   <eventitem eventid="A335">
    <eventdate>6.20.3425</eventdate>
    <title>Making Friends With The Venusian Flu Virus</title>
    <description>Dr. Biff Mercury discusses his theory on coexisting with useful organism.</description>
   </eventitem>
   <eventitem eventid="B963" optional="no">
    <eventdate>6.21.3425</eventdate>
    <title>Putting the Manners in Bedside Manner</title>
    <description>Dr. Zingzowpowie, the famous xenoneurosurgen lectures on Bedside Manner and the benefits of using cold tentacles during a physical</description>
   </eventitem>
   <eventitem eventid="C934" optional="yes">
    <eventdate>6.25.3425</eventdate>
    <title>An evening of Fun</title>
    <description>This evening join us for the monthly "Identify that Food" contest.</description>
   </eventitem>
  </events>
  <news>
   <newsitem itemnum="1">
    <newsdate>6.18.3425</newsdate>
    <title>End of the line for the incumber?</title>
    <body>
     The Universal News Network is reporting that <person>His Magnificence The Supreme Leader For Life</person>
     announced today that he has decided not to be cloned for a 14th term.
    </body>
   </newsitem>
   <newsitem itemnum="2">
    <newsdate>6.19.3425</newsdate>
    <title>New Fall Lineup</title>
    <body>
     The Omega Channel has announced two new shows for its new fall lineup.
     <program>Who's Running the Galaxy?</program>
     features a team of government scientists who accidentally clone two Supreme Leaders.
     If you think you're confused, imagine what the first family must be going through.
     <program>Trading Species</program> follows two temas of aliens who trade species
     and have only 48 hours to adjust and fool their neighbors.
    </body>
   </newsitem>
  </news>
 </content>

  context node가 content node 라면
   - newsdate element로 이동하기 위한 XPath 표현식은 news/newsitem/newsdate 이다.
   - events/eventitem/eventdate와 같은 경우는 문서 내 모든 eventdate element를 지칭한다.
   - content//title 과 같은 경우는 eventitem element 또는 newsitem element의 자식 element인 title element 모두를 가리킨다.
  news node가 context node인 경우 newsdate element로 이동하기 위해서는 newsitem/newsdate 와 같이 표기할 수 있다.
  //title과 같은 경우는 root element의 모든 후손 element 중에서 title element를 가리킨다.

2. 문서의 root
  절대 경로를 지원한다.
  newsdate element는 /content/news/newsitem/newsdate 과 같이 표기할 수 있다.

  / 는 한 문서의 root 이며 content 는 이 문서의 root element 이다.

  content는 문서 root(/)의 자식 element(/content)로 표현된다.

3. attribute
  attribute를 element의 자식으로 취급하지 않는다.
  XPath에서의 attribute 표현은 @를 이용한다.

  news element가 context node일 경우 다음과 같이 표현할 수 있다.

  /content/events/eventitem@eventid
  /content//newsitem@newsid
  newsitem@newsid

4. 검색조건
  element들의 그룹에서 특정 기준을 만족하는 element만 추려낼 수 있는 필터
  XPath에서는 []를 이용하여 특정 조건을 기술할 수 있다.

  예)
  attribute 값이 C934인 eventid attribute를 가지는 eventitem element를 선택하는 경우
  /content/events/eventitem[@eventid='C934']

  [] 내의 텍스트는 검색 대상과 검색 조건을 타나내며, 검색 조건읜 괄호 내 오른쪽에 표기한다.

  이 표현식의 처리 결과는 element 목록을 반환한다.

  문서내에서 특정 element에 대한 정보의 존재 유무를 확인하려면 다음과 같이 사용한다.

  /content/events/eventitem[@eventid]

# template
  XSLT 문서에서 모든 변환 과정은 하나 이상으 template으로 정의, 실행된다.
  template은 입력 문서에서 변환할 부분을 선택하고 출력 형식을 정의한다.

1. template의 생성
 
  <?xsml version="1.0"?>
  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/content/events/evenetitem">
    <h2>An event title</h2>
 <h3>(Date</h3>
 <p>An event description</p>
 <hr/>
  </xsl:template>
  <xsl:template match="/content/news">
  </xsl:template>
  </xsl:transform>

  match attribute는 template이 처리할 노드나 노드들을 지정하는데 이용된다.
  eventitem element를 방문할 때 마다 정보를 변환하여 출력한다.

2. template의 적용
  stylesheet에서는 하나의 template으로 문서를 변환하는 것보다 여러 개의 template으로 입력XML 문서의 정보들을 단계적으로 처리하는 것이 더 효과적이다.

 <xsl:template match="/">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="/content/events/eventitem/title">
  <h2><xsl:value-of select="."/></h2>
 </xsl:template>

 <xsl:template match="/content/events/eventitem/eventdate">
  <h3>(<xsl:value-of select="." />)</h3>
 </xsl:template>

 <xsl:template match="/content/events/eventitem/description">
  <p><xsl:value-of select="."/></p>
  <hr/>
 </xsl:template>
  apply-templates element를 이용해서 stylesheet에 정의된 다른 template을 적용할 수 있다.

  아래와 같이 하면 각 element의 순서를 입력문서에 나타나는 순서로 맞출 수 있다.

 <xsl:template match="/content/events/eventitem">
  <xsl:apply-templates select="title"/>
  <xsl:apply-templates select="eventdate"/>
  <xsl:apply-templates select="description"/>
  <hr/>
 </xsl:template>

 <xsl:template match="title">
  <h2><xsl:value-of select="."/></h2>
 </xsl:template>
 <xsl:template match="eventdate">
  <h2><xsl:value-of select="."/></h2>
 </xsl:template>
 <xsl:template match="description">
  <h2><xsl:value-of select="."/></h2>
 </xsl:template>

3. 내장 template
  template 규칙이 정의되지 않은 영역에 대해 내부적으로 적용되는 template을 내장 template으로 정의한다.

# 컨텐츠의 생성

1. 동적 element
  element element를 이용하여 생성할 수 있다.

  <xsl:template match="title">
    <xsl:element name="h2">              --+
   <xsl:value-of select="."/>           +- <h2><xsl:value-of select="title"/></h2> 와 같은 표현
 </xsl:element>                       --+
  <xsl:template>

  =>

  <h2>Making Friends with... </h2>

2. 동적 attribute
 element element로 생성한 element에 attribute를 추가하기 위해서는 attribute element를 이용한다.

  <xsl:template match="title">
    <xsl:element name="h2">
      <xsl:element name="a">
        <xsl:attribute name="href">register.php?event=<xsl:value-of select="../@eventid"/></xsl:attribute>
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  =>

  <h2><a href="register.php?event=A335">Making Friedns ...</a></h2>

3. XML에서 XML로의 변환
  XLST는 XML 문서를 다른 문서 구조를 가지는 XML 문서로 변환하기 위해 이용할 수 있다.

  <xsl:template match="/">
    <schedule>
   <xsl:apply-templates />
 </schedule>
  </xsl:template>

  <xsl:template match="content/events/eventitem">
    <xsl:element name="action">
   <xsl:attribute name="optinal"><xsl:value-of select="@optional"/></xsl:attribute>
   <title><xsl:value-of select="title"/></title>
 </xsl:element>
  </xsl:template>

  =>

  <!-- If an event is not explicitly marked as "optional", then it's mandatory -->

4. 주석문의 추가
  comment element를 이용한다.

  <xsl:comment>If an event is not explicitly marked as "optional", then it's mandatory</xsl:comment>

5. 처리 지시문
  XSLT를 이용해 문서 변환을 수행하는 application이 처리 지시문을 보려 할 경우, 이를 template에 직접 추가할 수는 없으나 명시적으로 생성할 수는 있다.

  <xsl:template match="content/events/eventitem">
    <xsl:processing-instruction name="updateSched">id="<xsl:value-of select="@eventid"/>"</xsl:processing-instruction>
 ...
  </xsl:template>

  =>

  <?updateSched id="A335'?>

6. 데이터의 정렬
  sort element를 제공

  <xsl:template match="/">
    <schedule>
   <xsl:comment>If an event is not explicitly marked as "optional", then it's mandatory</xsl:comment>
   <xsl:apply-templates select="content/events/eventitem">
     <xsl:sort select="eventdate" order="descending"/>
   </xsl:apply-templates>
 </schedule>
  </xsl:template>

7. 반복문의 생성
  for-each element를 이용

  <xsl:template match="/">
    <xsl:for-each select="eventitem">
   <xsl:apply-templates select="title"/>
 </xsl:for-each>
  </xsl:template>

8. copy-of와 value-of
  value-of : 해당 element의 값
  copy-of : element의 값 뿐만 아니라 element나 attribute를 포함한 node 집합의 전체 구조

  노드의 복사(eventitem element 단위로 출력 문서를 작성)
    <?xml version="1.0"?>
 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="content/events/eventitem">
     <xsl:copy-of select="."/>
   </xsl:template>
 </xsl:trasnform>

# 변수와 파라미터

1. named template
  template을 적용할 대상을 match attribute로 지정하지 않으며, template의 이름만 지정.
  이 template은 call-template element로 직접 적용한다.

  <xsl:template match="/content/events">
    <xsl:call-template name="eventTemplate"/>
  </xsl:template>

  <xsl:template name="eventTemplate">
    <xsl:for-each select="eventitem">
   <xsl:apply-templdate select="title"/>
   <xsl:apply-templdate select="eventdate"/>
   <xsl:apply-templdate select="description"/>
 </xsl:for-eachL
  </xsl:template>

  <xsl:template match="title">
  ...
  </xsl:template>
  ...

2. parameter
  parameter를 미리 정의하고 이를 나중에 참조할 수 있다.
  param element를 이용해 parameter를 생성한다.
  문서에서 parameter를 참조할 때 parameter의 이름 앞에 $ 기호를 붙인다.

  <xsl:param name="optionalChoice" select="'no'"/>

  <xsl:for-each select="eventitem[@optional=$optionalChoice]">
  ...
  </xsl:for-each>

3. named template and parameter
  template을 호출할 때 parameter를 설정할 수 있다.

  <xsl:call-template name="eventTemplate">
    <xsl:with-param name="isOptional"><xsl:value-of select="$optionalChoice"/></xsl:with-param>
  </xsl:call-template>
  <xsl:call-template name="eventTemplate">
    <xsl:with-param name="isOptional"><xsl:value-of select="$optionalChoice"/>x</xsl:with-param>
  </xsl:call-template>

  ...

  <xsl:template name="eventTemplate">
    <xsl:param name="isOptional" select="'no'"/>
 <xsl:for-each select="eventitem[@optiona=$isOptional]">
   <xsl:apply-templates select="title"/>
   ...
 </xsl:for-each>
  </xsl:template>
  </xsl:call-template>

4. 변수
  parameter와 다르게 사용된 이후에 새로운 값으로 설정이 가능하다.

  변수 정의의 문법
  1. select attribute를 이용해 변수 값을 직접 설정할 수 있다.
  2. variable element의 contents를 설정함으로써 변수 값을 설정할 수 있다.

  <xsl:variable name="optionalChoiceValue" select="$optionalChoice"/>
  <xsl:call-template name="eventTemplate">
    <xsl:with-param name="isOptional"><xsl:value-of select="$optionalChoiceValue"/></xsl:with-param>
  </xsl:call-template>

# 흐름제어
  XSLT는 변환 엔진이 어떤 template을 처리해야 하는지 명시할 수 있도록 흐름 제어 기능을 제공한다.

1. if-then element

if element는 명령어 치리를 위한 조건을 가질 수 있도록 test attribute를 갖는다.
  <xsl:element name="div">
    <xsl:if test="$isOptional='no'">
   <xsl:attribute name="style">color:red</xsl:attribute>
    </xsl:if>
 ...
  </xsl:element>

2. choose element
  if element를 이용하여 처리할 수 없는 else 문제 해결
  choose element는 잘 구성된 element들로 표현되는 분기 조건들을 담을 수 있는 container를 제공.
  하나 이상의 when element를 포함.
  when element는 if element와 유사한 동작 수행.
  otherwise element를 통해 when 문장들을 참으로 만족시키지 않는 다른 모든 조건들을 처리할 수 있도록 함.

  <xsl:call-template name="eventTemplate">
    <xsl:with-param name="isOptional">
   <xsl:value-of select="$optionalChoice"/></xsl:with-param>
  </xsl:call-template>

  <xsl:choose>
    <xsl:when test="$optionalChoice='yes'">
   <xsl:call-template name="eventTemplate">
     <xsl:with-param name"isOptional">no</xsl:with-param>
 </xsl:when>
    <xsl:when test="$optionalChoice='no'">
   <xsl:call-template name="eventTemplate">
     <xsl:with-param name"isOptional">yes</xsl:with-param>
 </xsl:when>
 <xsl:otherwise>
   <!-- If a yes or no value was not originally specified, assume it should have been no. That means the opposite is yes. -->
   <xsl:call-template name="eventTemplate">
     <xsl:with-param name="isOptional">yes</xsl:with-param>
   </xsl:call-template>
 </xsl:otherwise>

# 출력 모드의 설정
  mode attribute를 이용하여 어떤 template이 이용되어야 하는지 그리고 어떤 template이 처리되어야 하는지 지정할 수 있다.

  ...
  <xsl:apply-templates select="eventitem[$optional='no']" mode="no"/>
  ...

  <xsl:template match="eventitem" mode="yes'>
    <xsl:element name="div">
 ...
 </xsl:element>
  </xsl:template>

  ...

'그거 > Tech' 카테고리의 다른 글

자바서비스넷의 기술자료 링크  (0) 2007.02.23
10. XML 문서의 변환과 응용  (0) 2007.02.22
08.XML Schema  (0) 2007.02.16
07.DTD(Document Type Definition)  (0) 2007.02.15
06. XML 문서에 대한 유효성 검증  (0) 2007.02.15
:
Posted by 뽀기