자자 자자자자 자자 자자 자자자자 자자 XML XML Chap7. XSLT Chap7. XSLT (XML Stylesheet Langu (XML Stylesheet Langu age Transformations) age Transformations) 김김김 김김김
Mar 20, 2016
자바 개발자를 위한 자바 개발자를 위한 XMLXMLChap7. XSLTChap7. XSLT(XML Stylesheet Language Tran(XML Stylesheet Language Tran
sformations)sformations)
김만수김만수
22
CHAPTHER 7CHAPTHER 7 XSLTXSLT(XML Stylesheet Language Transformations)(XML Stylesheet Language Transformations)
XSLT XSLT 소개소개 XSLT XSLT 원소들원소들
33
7.1 XSLT 7.1 XSLT 소개소개 XSL(XML Stylesheet Language)XSL(XML Stylesheet Language)
CSSCSS 와 더불어 와 더불어 XML XML 문서의 스타일을 정의하기 위한 언어 문서의 스타일을 정의하기 위한 언어 XSL XSL 구성구성
XSLT(XSL Transformations)XSLT(XSL Transformations) XSL-FO(XSL Formatting Objects) XSL-FO(XSL Formatting Objects)
XMLXML 의 문법을 준수한 의 문법을 준수한 XML XML 문서문서 XSLTXSLT
XML XML 문서를 다른 형태의 문서를 다른 형태의 XML XML 문서나 다른 형태의 문서문서나 다른 형태의 문서 (( 예예 :HT:HTML, ML, 텍스트텍스트 )) 로 변환 가능로 변환 가능
XSLT + XSL-FOXSLT + XSL-FO XMLXML 문서를 문서를 PDFPDF 와 와 RTF RTF 등의 문서로 변환 가능등의 문서로 변환 가능
44
7.1 XSLT 7.1 XSLT 소개소개
XMLXML문서문서
XSLTXSLT처리기처리기
XML XML 문서문서
HTML HTML 문서문서
DBDB
WMLWMLVoiceXMLVoiceXML
XSLTXSLT처리기처리기RTFRTF
PDFPDF
XSLT+FOXSLT+FOStyle SheetStyle Sheet
XSLTXSLTStyle SheetStyle Sheet
55
7.1 XSLT 7.1 XSLT 소개소개 XSLTXSLT
XML XML 문서를 변환하기 위해서 변환 규칙들을 기술문서를 변환하기 위해서 변환 규칙들을 기술 변환 규칙 변환 규칙 템플릿 템플릿 (Template)(Template) TemplateTemplate
<template> element<template> element 로 기술로 기술 <template><template> 의 내용은 문서를 변환하기 위한 명령어와 내용들의 내용은 문서를 변환하기 위한 명령어와 내용들 Math Math 속성을 가짐속성을 가짐
규칙이 적용될 규칙이 적용될 XML XML 원소를 찾기 위해 기술원소를 찾기 위해 기술 값으로 값으로 XPathXPath 를 사용를 사용
XSLT XSLT 문서의 최상위 문서의 최상위 elementelement 는 는 <stylesheet><stylesheet> Namespace : http://www.w3.org/1999/XSL/TransformNamespace : http://www.w3.org/1999/XSL/Transform
66
7.1 XSLT 7.1 XSLT 소개소개 XSLTXSLT
XSLT XSLT 파일 형태파일 형태 (( 문서 구조문서 구조 ))
<?xml version='1.0' encoding='euc-kr'?>
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">. . . . .
</xsl:template>
<xsl:template match=“contact">. . . . .
</xsl:template>
. . . . .
</xsl:stylesheet>
TemplateTemplate
매칭 규칙매칭 규칙
처리 내용처리 내용
77
3. Namespace4. XSLT 처리 후 결과물의 encoding 을 euc-kr
5. Root element 를 찾으면 아래 내용을 출력 즉 , <memo> element 를 찾으면 …11. Root element(<memo>) 부터 시작하여 child element 에 대해서 template 를 적용
15. <memo> element 를 찾으면16. <memo> element 의 내용을 출력하고 , child element 에 대해서 template 를 적용18. <contents> element 를 찾으면19. <P> 를 출력하고 ,20. <contents> element 의 내용을 출력하고 , child element 에 대해서 template 를 적용21. <contents> 의 child element 에 대한 template 적용이 모두 끝나면 , </P> 와 <HR/> 를 출력
99
7.1 XSLT 7.1 XSLT 소개소개
memo
to date contents from
who 홍 2001.. 여자 .. 허준
Template Template 적용 순서적용 순서/
1010
7.1 XSLT 7.1 XSLT 소개소개 XSLT XSLT 실행 방법실행 방법
Java2 v1.4 Java2 v1.4 환경에서환경에서
Batch Batch 파일 작성파일 작성
java org.apache.xalan.xslt.Process memo.xml memo.xsl memo.htmljava org.apache.xalan.xslt.Process memo.xml memo.xsl memo.html
XSLT 처리 클래스
XSL 파일 (Style Sheet)
결과 파일
XML 파일 ( 데이터 )
@echo off@echo offrem Xalan XSLT rem Xalan XSLT 프로세서프로세서java org.apache.xalan.xslt.Process -IN %1 -XSL %2 -OUT %3java org.apache.xalan.xslt.Process -IN %1 -XSL %2 -OUT %3
xslt.bat
1111
7.1 XSLT 7.1 XSLT 소개소개 XML XML 문서에 바로 정의문서에 바로 정의
XSLT XSLT 기능을 내장한 애플리케이션에서는 바로 처리 됨기능을 내장한 애플리케이션에서는 바로 처리 됨 Internet Explorer 6.0 Internet Explorer 6.0 에서 실행에서 실행
XML 문서에 xsl 파일 URI 지정
1212
7.2 XSLT 7.2 XSLT 원소들원소들 순서순서
기본적인 원소들기본적인 원소들 제어문 형태 원소들제어문 형태 원소들 XML XML 문서 생성문서 생성 기타 원소들기타 원소들 XSLT XSLT 관련 함수들관련 함수들
1313
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <stylesheet> element<stylesheet> element
XSLTXSLT 의 의 ROOT elementROOT element ElementElement 내에는 다양한 내에는 다양한 XSLT XSLT 원소들을 포함원소들을 포함 <transform> element<transform> element 를 를 <stylesheet> element <stylesheet> element 대신 대신
사용 가능사용 가능
<xsl:stylesheetid = idextension-element-prefixes = tokensexclude-result-prefixes = tokensversion = number>
<!-- Content: (xsl:import*, top-level-elements) -->
<xsl:stylesheet>
형식Stylesheet 에서 사용되는 확장 원소의 접두어를 기술
불필요한 namespace 를 제거할필요시 해당 namespace 를 기술
1414
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <stylesheet> element<stylesheet> element
top-level top-level 원소들원소들 xsl:importxsl:import xsl:includexsl:include xsl:strip-spacexsl:strip-space xsl:preserve-spacexsl:preserve-space xsl:outputxsl:output xsl:keyxsl:key xsl:decimal-formatxsl:decimal-format xsl:namespace-aliasxsl:namespace-alias xsl:attribute-setxsl:attribute-set xsl:variablexsl:variable xsl:paramxsl:param xsl:templatexsl:template
child elementchild element 의 순서 무시의 순서 무시
항상 처음 부분에 위치 해야 함항상 처음 부분에 위치 해야 함
1515
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <template> element<template> element
XSLTXSLT 에서 가장 중요한 원소에서 가장 중요한 원소 XSLTXSLT 에서 문서를 변환하기 위한 작업을 기술에서 문서를 변환하기 위한 작업을 기술 구성구성
주어진 주어진 XML XML 문서 내 원하는 부분을 찾기 위한 패턴 정보 문서 내 원하는 부분을 찾기 위한 패턴 정보 XPathXPath로 기술로 기술
변환된 문서에 추가할 데이터와 명령어변환된 문서에 추가할 데이터와 명령어
<!-- category: top-level-element --><xsl:template match = pattern name = qname
priority = number mode = qname>
<!-- Content: (xsl:param*, template) -->
<xsl:template>
형식
template 을 어느 원소에 적용할 것인지를 기술 template 의 우선 순위를 기술
1616
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <apply-templates> element<apply-templates> element
child nodechild node 를 처리하기 위해 명시적으로 기술를 처리하기 위해 명시적으로 기술 특정 특정 child nodechild node 를 선택하려면 를 선택하려면 select select 속성을 기술속성을 기술
<!-- category: instruction --><xsl:apply-templates
select = node-set-expressionmode = qname>
<!-- Content: (xsl:sort | xsl:with-param)* -->
< xsl:apply-templates >
형식
<xsl:template match=“AuthorList”>저자목록<xsl:apply-templates select=“Author”/>
</xsl:template>
예 :author element 에 template 적용<xsl:template match=“AuthorList”>
저자 성명<xsl:apply-templates select=“Author/Name”/>
</xsl:template>
예 :author element 에 template 적용
1717
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <value-of> element<value-of> element
XML XML 문서에 포함된 원소의 내용을 문자열 형태로 접근문서에 포함된 원소의 내용을 문자열 형태로 접근<!-- category: instruction --><xsl:value-of
select = string-expressiondisable-output-escaping = “yes” | “no” />
형식
<xsl:template match=“text()”><xsl:value-of select=“.”/>
</xsl:template>
예 : 현재 노드의 텍스트 데이터를 출력 XML element 가 아닌 텍스트 데이터만 찾음현재의 노드를 선택
1818
7.2.1 7.2.1 기본적인 원소들기본적인 원소들 <output> element<output> element
XSLT XSLT 결과로 생성되는 문서를 결과로 생성되는 문서를 customizecustomize
<!-- category : top-level-element --><xsl:output
method = “xml” | “html” | “text” | qname-but-not-ncnameversion = nmtokenencoding = stringomit-xml-declaration = “yes” | “no”standalone = “yes” | “no”doctype-public = stringcdata-section-element = qnamesindent = “yes” | “no”media-type = string/>
형식
<xsl:output method=“html” version=“3.2” encoding=“euc-kr”/>
예
XML 선언 부분이 결과 문서에 포함될 것인지 여부를 기술결과 문서의 DTD 를 기술
1919
7.2.2 7.2.2 제어문 형태 원소들제어문 형태 원소들 <for-each> element<for-each> element
select select 속성을 통해 선택된 노드에 반복적으로 속성을 통해 선택된 노드에 반복적으로 templatetemplate 를 적용를 적용 프로그래밍 언어에서 프로그래밍 언어에서 for for 문과 유사문과 유사
<!-- category : instruction --><xsl:for-each select = node-set-expression>
<!-- Content: (xsl:sort*, template) -->
</xsl:for-each>
형식
2020
7.2.2 7.2.2 제어문 형태 원소들제어문 형태 원소들 <if> element<if> element
조건적으로 조건적으로 templatetemplate 를 적용시 사용를 적용시 사용 test test 속성 값이 속성 값이 truetrue 일 경우 일 경우 templatetemplate 이 적용이 적용 test test 속성 값속성 값
XPath XPath 함수함수 식식
<!-- category : instruction --><xsl:if test = boolean-expression>
<!-- Content: (template) -->
</xsl:if>
형식
<xsl:template match = “namelist/name”><xsl:apply-templates><xsl:if test=“not(position() = last()”>, </xsl:if>
</xsl:template>
예 : 마지막 노드가 아닌 경우에 “ ,” 를 출력<xsl:template match = “item”>
<tr><xsl:if test = “position() mod 2 = 0”>
<xsl:attribute name=bgcolor”>yellow
<xsl:attribute><xsl:if><xsl:apply-templates/></tr>
</xsl:template>
예 : 노드의 위치에 따라 다른 값을 출력
2121
7.2.2 7.2.2 제어문 형태 원소들제어문 형태 원소들 <choice>, <when>, <otherwise> element<choice>, <when>, <otherwise> element
여러 가지 경우 중에서 조건을 만족하는 한 원소를 처리여러 가지 경우 중에서 조건을 만족하는 한 원소를 처리 C LanguageC Language 에서 에서 switch-case switch-case 문 형태와 유사문 형태와 유사
<!-- category : instruction --><xsl:choose>
<!-- Content: (xsl:when+, xsl:otherwise?) --></ xsl:choose >
형식
<!-- category : instruction --><xsl:when test = boolean-expression>
<!-- Content: template --></ xsl:when>
형식<!-- category : instruction --><xsl:otherwise>
<!-- Content: template --></ xsl:otherwise >
형식
when 에 맞는 조건이 없는 경우 처리C Language : default:
2323
7.2.2 7.2.2 제어문 형태 원소들제어문 형태 원소들 <call-template> element<call-template> element
name name 속성값이 지정한 속성값이 지정한 templatetemplate 를 호출를 호출 parameterparameter 가 필요할 경우 가 필요할 경우 <with-param> element <with-param> element 사용사용
<!-- category : instruction --><xsl:call-template name = qname>
<!-- Content: xsl:with-param* --></ xsl:call-template>
형식 : <call-template>
<!-- category : top-level-element --><xsl:with-param name = qname select = expression>
<!-- Content: template --></ xsl:param>
형식 : <with-param>
P.7-22 P.7-22 참조참조
2424
7.2.2 7.2.2 제어문 형태 원소들제어문 형태 원소들 <variable> element<variable> element
변수를 지정하기 위해 사용변수를 지정하기 위해 사용 한번 값이 할당되면 변경 안됨한번 값이 할당되면 변경 안됨 변수 이름 앞에 변수 이름 앞에 $$ 기호 붙임기호 붙임
<!-- Category: top-level-element --><!-- Category: instruction --><xsl:variable name = qname select = expression> <!-- Content: template --></xsl:variable>
형식
2525
7.2.3 XML 7.2.3 XML 문서 생성문서 생성 <element> element : element <element> element : element 생성생성
결과 결과 treetree 에 새로운 에 새로운 XML XML 원소를 생성원소를 생성
<!-- Category: instruction --><xsl:element name = { qname } namespace = { uri-reference } use-attribute-sets = qnames> <!-- Content: template --></xsl:element>
형식
<item> 에 있는 모든 모든 속성들에 대해서
새로 생성할 element 의 이름을 현재 속성의 이름으로 지정
2626
7.2.3 XML 7.2.3 XML 문서 생성문서 생성 <attribute> element : <attribute> element : 속성 생성속성 생성
elementelement 의 속성을 생성의 속성을 생성
<attribute-set> element<attribute-set> element 여러 개의 속성을 기술여러 개의 속성을 기술
<!-- Category: instruction --><xsl:attribute name = { qname }
namespace = { uri-reference }><!-- Content: template -->
</xsl:attribute>
형식
<!-- Category: top-level-element --><xsl:attribute-set name = qname
use-attribute-sets = qnames><!-- Content: xsl:attribute* -->
</xsl:attribute-set>
형식
2727
7.2.3 XML 7.2.3 XML 문서 생성문서 생성
모든 모든 elementelement 의 이름을 의 이름을 <item> <item> 속성 속성 이름으로 변경하고이름으로 변경하고 , element, element 의 의 text text 노드 노드 값을 생성된 속성의 값으로 설정값을 생성된 속성의 값으로 설정
2828
7.2.3 XML 7.2.3 XML 문서 생성문서 생성 <text> element : <text> element : 텍스트 생성텍스트 생성 <processing-instruction> element : <processing-instruction> element : 처리 명령어 생성처리 명령어 생성 <comment> element : <comment> element : 주석문 생성주석문 생성 복사하기복사하기
소스 소스 treetree 의 내용을 그대로 결과 의 내용을 그대로 결과 treetree 에 복사에 복사 <copy><copy>
현재 현재 nodenode 를 복사를 복사 <copy-of><copy-of>
현재 현재 nodenode 와 그 이하 모든 자식 와 그 이하 모든 자식 nodenode 를 결과 트리에 복사를 결과 트리에 복사
2929
7.2.3 7.2.3 기타 원소들기타 원소들 <include> element<include> element
XSLT XSLT 파일에서 다른 파일에서 다른 XSLT XSLT 파일을 포함 시킴파일을 포함 시킴 <stylesheet><stylesheet> 의 의 child elementchild element 로만 사용로만 사용 <include> element<include> element 가 사용된 위치에 지정된 가 사용된 위치에 지정된 XSLT XSLT 파일의 내용이 포함 파일의 내용이 포함
<!-- Category: top-level-element --><xsl:include href = uri-reference />
형식
table.xsl list.xsl
a.xsl b.xsl c.xsl
3131
7.2.3 7.2.3 기타 원소들기타 원소들 <import> element<import> element
<include><include> 와 비슷와 비슷 <include><include> 는 중복 정의 허용 안함 는 중복 정의 허용 안함 <import><import> 는 중복 허용는 중복 허용
<apply-imports> element<apply-imports> element 내부의 정의 내용이 외부 내부의 정의 내용이 외부 <import> <import> 된 내용을 재 정의 하는 경우 된 내용을 재 정의 하는 경우
외부 파일의 정의를 사용외부 파일의 정의를 사용 <strip-space> element<strip-space> element
XML XML 문서의 불필요한 공백 문자를 삭제문서의 불필요한 공백 문자를 삭제 <preserve-space> element<preserve-space> element
XML XML 문서의 공백 문자를 그대로 두어야 하는 경우문서의 공백 문자를 그대로 두어야 하는 경우
3232
7.2.3 7.2.3 기타 원소들기타 원소들 <number> element<number> element
XML XML 문서에서 자동적으로 번호를 부여문서에서 자동적으로 번호를 부여<!-- Category: instruction --><xsl:number level = "single" | "multiple" | "any"
count = pattern from = pattern value = number-expression format = { string }lang = { nmtoken }letter-value = { "alphabetic" | "traditional" }grouping-separator = { char }grouping-size = { number } />
형식
3434
7.2.3 7.2.3 기타 원소들기타 원소들 <sort> element<sort> element
XML XML 문서의 내용을 정렬문서의 내용을 정렬 <apply-templates> or <for-each> element<apply-templates> or <for-each> element 의 의 child child
elementelement 로 사용될 수 있음로 사용될 수 있음<xsl:sort
select = string-expression lang = { nmtoken }data-type = { "text" | "number" | qname-but-not-ncname }order = { "ascending" | "descending" }case-order = { "upper-first" | "lower-first" } />
형식