Upload
vuonganh
View
225
Download
1
Embed Size (px)
Mokpo National University
Distributed Computing Lab1
XPath/XSLT 이해
Understanding
XML Path Language and XSL Transformation
2001. 6. 28최 한 석
목포대학교 정보공학부 / ㈜ 한국지식웨어 R&D 연구소ⓒAll copyright reserved
Mokpo National University
Distributed Computing Lab2
목 차
XSLT, XPath, XSL-FO 배경
XSLT 변환/프로세싱 모델
XSLT Instruction 엘리먼트
XPath 이해
XPath 표현식
XPath 적용 예
XSL-FO 이해
XSL-FO 포맷팅 모델
XSL-FO Layout 체계
요약
Mokpo National University
Distributed Computing Lab3
XSLT, XPath, XSL-FO 배경 (1)
XML (eXtensible Markup Language)• 1998년 W3C에서 채택한 Web 표준 확장된 마크업 언어
• 문서 정보의 인스턴스를 사용자가 정의한 형식적 규칙(Rules)에의해 계층적으로 기술
• 문서의 구조, 내용, 표현이 분리 : “XML Philosophy”• 응용간의 데이터 자유로운 전송 및 변환
XSL (XML StyleSheet Language)• 1998년 및 2000년 XSL Working Draft 발표, 수정, 보완 계속
• 문서 변환 명령 및 포맷팅 Semantics와 Syntax를 정의한 언어
• XML 문서 구조 변환 : XSLT
• XML 문서를 다른 문서로 Rendering : XSL-FO
• 최신 XML Linking과 Style 접목 시도
Mokpo National University
Distributed Computing Lab4
XSLT (XSL Transformation Language)• XML로 인코딩된 문서를 다른 문서 모델로 구조 변환하는 Semantics 정의
• 스타일시트 표현을 위한 Construction Rule 제공
• 1999년 11월 XSLT V1.0규격 발표, 2001년 2월 V2.0 WD 발표
XPath (XML Path Language)• XSLT와 XPointer 기반의 XML 문서 요소들에 대한 구조적인 Addressing 기법 제공
• Tree형식의 XML 엘리먼트들의 위치 정보 및 Expression 규격 정의
• 1999년 11월 XPath V1.0 규격 발표, 2001년 2월 V2.0 WD 발표
XSL-FO(XSL Formatting Objects)• XSLT를 적용하여 변환된 XML 문서의 Visual Layout 모델 정의
• XML 응용 문서의 표현 규격 제공 (Flow objects와 Layout Objects들로 구성)
• 2000년 11월 XSL V1.0 규격에서 정의
XML Linking & Style• XLink 엘리먼트와 styling에 관한 상호작용 개념 모델 제공
• 최근 W3C 기술문서를 사용하여 응용방안 제시
• 2001년 6월초 W3C Note 발표
XSLT, XPath, XSL-FO 배경 (2)
Mokpo National University
Distributed Computing Lab5
Related Terms• 이름공간 (Namespace)
서로 다른 정보 소스의 출처 및 출원지(URI)의 semantic 정보 표현
xmlns : xsl = http://www.w3.org/TR/WD-xsl
• DTD (Document Type Definition) 정보의 문서 구조 정의
• XML-Schema XML 문서 내용에 대한 데이터 형식 정의
• CSS ( Cascading Style Sheet Language) HTML 에 적용되는 스타일시트 언어
• DSSSL (Document Style Semantics and Specification Language)
SGML에 적용되는 스타일 시트 언어
XSLT, XPath, XSL-FO 배경 (3)
Mokpo National University
Distributed Computing Lab6
XSLT 변환 / 프로세싱 모델 (1)
XSLT 역할• 서로 다른 애플리케이션간의 데이터 또는 구조 변환
XML로부터 필요한 데이터 추출
서로 다른 애플리케이션에서 데이터 합병
• 정보의 서로 다른 표현 (Display, Print, Formatting)
XML2HTML, XML2PDF, XML2Audio, XML2XML, XML2Others
God saveOur gracious
Queen
<SONG><KEY sig=“G” /><TIME Sig=“3/4” /><STAVE><BAR Number=“1” /><CLEF Name=“G” /><CHORD><NOTE Pitch=“b” >……..</SONG>
XSLT
XSLT
XML 문서
XSLT
Mokpo National University
Distributed Computing Lab7
XSLT 변환 모델
XSLT 변환 / 프로세싱 모델 (2)
SourceDocument
StyleSheet
TransformationProcess
XSLTProcessor
NON-XML
XML
Text/HTML
SourceTree
StylesheetTree
ResultTree
Mokpo National University
Distributed Computing Lab8
XSLT 프로세싱 모델
XSLT 변환 / 프로세싱 모델 (3)
Source
StyleSheetXSLT/XSL
XSLFormatting
Objects
Display
HTML/TEXT
Instruction Execution
SourceNode Tree(pattern)
StyleSheetNode Tree(Template) Result
NodeTree
XSLT Engine
XML
Legend
XSLFormatting
Objects
XSLFormatting
Objects
Aural
XML
NonXML
NonXML CVML
XML
Result
SourceNode
StyleSheetResult Node
StylesheetInstruction Node
Mokpo National University
Distributed Computing Lab9
XSLT Instruction 엘리먼트
템플릿 생성 및 노드 적용 명령 엘리먼트• <xsl : template match = “/”>• <xsl : apply-templates / >
노드 선택 및 값처리 명령 엘리먼트• <xsl : value-of select = “ ” / > : 노드 선택 후 값 처리
• <xsl : for-each select = “ ” > : 다중 선택
Match 속성의 패턴 지정• Root node 와 일치 : <xsl : template match = “ / ”>• 엘리먼트와 일치 : <xsl : template match = “ PLANET ”>• 특정자식과 일치 : <xsl : template match = “ PLANET/NAME”>• 엘리먼트 모든 자손과 일치 ; <xsl : template match = “ PLANET//NAME”>• 속성과 일치 : <xsl : template match = “MASS ”>
<xsl : value-of select = “@UNITS ” / >
• ID로 일치 : <xsl : template match = “ id( ‘choi’ ) ”> : 함수적용
• 코멘트와 일치 : <xsl : template match = “ comment ( )”>
Mokpo National University
Distributed Computing Lab10
• 텍스트 노드 일치 : <xsl : template match = “ text ( ) ”>• 프로세싱 명령 일치 :
<xsl : template match = “ /processing-instruction (?xml-like) ”>
• Or 연산자 이용 : <xsl : template match = “ name | mass ”>• [ ] 연산 이용 :
<xsl : template match = “ PLANET [@COLOR = “BLUE”] ”>
Select 속성으로 패턴 지정• Xpath 표현식 사용, XSLT 성능 향상
• 노드선택 이외에 부울 함수, 연산함수, 문자열, 위치, 경로 선택
• <xsl : template match = “ PLANET[not(position( ) = last ( ) )] ”><xsl : value-of select = “ . ” / >
</xsl : template >
기타 XSLT Instruction 엘리먼트
XSLT Instruction 엘리먼트
Mokpo National University
Distributed Computing Lab11
Top-level Instruction Elements – (1)
<xsl:apply-imports> <xsl:element>
<xsl:apply-templates> <xsl:fallback>
<xsl:attribute> <xsl:for-each>
<xsl:attribute-set> <xsl:if>
<xsl:call-template>
<xsl:choose>
<xsl:comment>
<xsl:copy>
<xsl:copy-of>
<xsl:decimal-format>
XSLT Instruction 엘리먼트
Mokpo National University
Distributed Computing Lab12
Top-level Instruction Elements – (2)
<xsl:import> <xsl:processing-instruction>
<xsl:include> <xsl:sort>
<xsl:key> <xsl:strip-space>
<xsl:message> <xsl:stylesheet>
<xsl:namespace-alias> <xsl:template>
<xsl:number> <xsl:text>
<xsl:otherwise> <xsl:transform>
<xsl:output> <xsl:value-of>
<xsl:param> <xsl:variable>
<xsl:preserve-space> <xsl:when>
<xsl:with-param>
XSLT Instruction 엘리먼트
Mokpo National University
Distributed Computing Lab13
• PLANETS XML 문서
XSLT 변환 예제
<?xml version="1.0" encoding="euc-kr" ?><?xml-stylesheet type="text/xsl" href=“PLANETS.xsl" ?><PLANETS><PLANET COLOR="RED"><NAME>Mercury</NAME><MASS UNITS="(Earth=1)"> .0553 </MASS><DAY UNITS="days"> 58.65 </DAY><DISTANCE UNITS="million miles"> 43.3 </DISTANCE>
</PLANET><PLANET COLOR="WHITE" ><NAME>Venus</NAME><MASS UNITS="(Earth=1)"> .815 </MASS><DAY UNITS="days"> 116.75 </DAY><DISTANCE UNITS="million miles"> 66.8 </DISTANCE>
</PLANET></PLANETS>
PLANETS.xsl
XML2HTML변환
IE Viewing화면
XPath 이해
XSLT 적용 예
Mokpo National University
Distributed Computing Lab14
<?xml version="1.0" encoding="KSC5601" ?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/" ><html><head></head><body>
<xsl:apply-templates select="PLANETS/PLANET[@COLOR='WHITE']" /></body></html>
</xsl:template><xsl:template match="PLANET" ><ul><li><h2><xsl:value-of select="NAME" /></h2></li><li><h3><xsl:apply-templates select="./*/@UNITS" /></h3></li></ul>
</xsl:template><xsl:template match="@UNITS">
<xsl:value-of select="." /><br /></xsl:template></xsl:stylesheet>
PLANETS XSL문서
XML2HTML 변환
IE Viewing화면
PLANETS.xml
XPath 이해
XSLT 적용 예
Mokpo National University
Distributed Computing Lab15
<html><head></head><body>
<ul><li><h2> Venus </h2></li><li><h3> (Earth=1) </h3>
<h3> days </h3><h3> million miles </h3>
</li></ul>
</body><html>
XML2HTML 변환
IE Viewing화면
PLANETS.xml
PLANETS.xsl
XPath 이해
XSLT 적용 예
Mokpo National University
Distributed Computing Lab16
XML2HTML 변환
PLANETS.xml
PLANETS.xsl
XML2HTML 변환
XPath 이해
XSLT 적용 예
Mokpo National University
Distributed Computing Lab17
XPath 이해 (1)
XPath 배경• XSLT의 Sub Language로서 역할 정의
• XSLT 수식 문법 및 XPointer 사이의 중복성 문제 해결
• XSLT 노드 선택에 필요한 절대 경로 및 상대경로 지정
• XPath는 수식 연산, 부울 처리, 스트링 처리 함수 제공
• 1999년 V1.0 2001년 6월, V2.0 WD 발표
XPath 이해• XPath는 node 집합의 위치 경로 표시
• XPath 데이터 모델 : 축(Axis) + 노드테스트 + 술어 (Predicates Expression)<xsl : value-of select = “child :: NAME [position ( ) = 5] ” >
다른 예<xsl : value-of select = “sum(//book/@price) div count (//book) ” / >
<xsl : value-of > :XSLT 명령어, select된 엘리먼트의 value를 문서에 writing.
sum ( ) : book요소들의 price속성 값의 합.
count ( ) : book 요소들의 갯수
div : 나누기 연산자
Mokpo National University
Distributed Computing Lab18
XPath 표현식
XPath 노드 테스트
• Node 의 이름 사용, *(wild card) 사용
• 함수 사용 : comment ( ), node ( ), text ( ), processing-instruction ( )
XPath 노드 집합
• 노드 엘리먼트 이름 사용
• 노드 동작 함수 : last( ), position( ), count(node-set), id(string),
local-name(node-set), namespace-uri(node-set), name(node-set)
XPath 부울 식 ; !=, <, <=, =, >, >=
XPath 수식 : +, -, *, div, mod, celiling( ), floor( ), round( ), sum( )
XPath 문자열 ; starts-with(st1, st2), substring(st1, offset, length)
,concat(st1, st2)
Mokpo National University
Distributed Computing Lab19
기타 함수
– boolean()
– ceiling()
– concat()
– contraints()
– count()
– current()
– document()
– element-available()
– false()
– floor()
– format-number()
– function-available()
– generate-id()
— last()
— local-name()
— name()
— namespace-uri()
— normalize-space()
— not()
— number()
— position()
— round()
— starts-with()
— string()
— string-length()
– unparsed-entity-uri()
– system-property()– substring-before()
– substring-after()
– substring()
– sum()
– translate()
– true()
– id()
– key()
– lang()
XPath 표현식
Mokpo National University
Distributed Computing Lab20
XPath 표현식
XPath 경로 축
• AxisName : self, ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self, following, following-sibling, namespace, preceding-sibling
• Axis specifier : ::
• Location path : /(절대경로 루트), //(상대경로, 현재노드 및 자손), .(노드 자신), ..(현재 노드의 부모)
Preceding
Preceding-sibling
Descendant
Child
Descendant
Child
Self Follwing-sibling
Parent Following
Ancestor
Ancestor
Ancestor-or-self
FollowingPreceding
Descendant-or-self
Mokpo National University
Distributed Computing Lab21
XPath 적용 예
<?xml version="1.0" encoding="KSC5601" ?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/" ><html><head></head><body><xsl:apply-templates select="PLANETS/PLANET[not(position()=last())]" /></body></html></xsl:template><xsl:template match="PLANET" ><ul><li><h2><xsl:value-of select="child::NAME" /></h2></li><li><h3>MASS+DAY : <xsl:value-of select="MASS+DAY" /></h3></li></ul></xsl:template></xsl:template></xsl:stylesheet>
PLANETS XSL문서
XML2HTML, IE Viewing XSL-FO 이해
Mokpo National University
Distributed Computing Lab22
XPath 적용 예
• XML2HTML
PALENTS.xsl XSL-FO 이해
• IE Viewing
<html><head></head><body>
<ul><li><h2> Mercury</h2></li><li><h3>MASS+DAY:58.7053 </h3></li></ul>
</body><html>
Mokpo National University
Distributed Computing Lab23
XSL-FO 이해
XSL-FO 기본 개념
• 포맷팅 객체 관련 NameSpace
http://www.w3.org/1999/xsl/Format
http://www.w3.org/TR/xsl
http://www.w3.org/TR/xsl/sliceb.html
• XSL-FO는 XSLT tree 변환 후, 문서의 paging, layout, formatting 정보 부여
• XSLT에서 생성된 Result tree 로부터 포맷팅을 위한 Area Tree 모델 생성
• 56개의 formatting objects 및 177개의 포맷팅 특성 사용
• FO는 block level(vertical)과 inline level(horizonal)로 구분
• XSLT 프로세서 적용 후 FOP 프로세서를 통해 특성 문서 포맷을 변환
• 현재 활용가능 FOP는 Arpache XML2PDF FOP
XML2PDF 변환 과정
XML Source Tree
XSL
XML Tree
XSLT
XSLT Processor
Applying FO FOP
PDF Results
Document
Result
Tree
FO2PDF
Mokpo National University
Distributed Computing Lab24
XSL-FO 포맷팅 모델
Pagination 및 Layout 구성 모델
Region-before
Region-after
Region-start
Region-end
Region-body
Margin-right
Margin-left
Margin-bottom
Margin-top
Page-width
Page-height
Margin(transparent)
border
padding
content
Element width
Box width
• XSL-FO 포맷팅 모델 • CSS 포맷팅 모델
Mokpo National University
Distributed Computing Lab25
Pagination 및 Layout FO 트리 체계
fo:root
fo:layout-master-set fo:page-sequencefo:declaration
fo:color-profile
fo:simple-
page-master
fo:page-sequence-master fo:title
fo:static-content
fo:flow
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab26
Pagination 및 Layout FO 명령어
•fo:layout-master-set페이지의 Layout과 Sequencing을 정의한다.
• fo:page-master페이지 구분 및 그 영역을 기술한다.
• fo:page-sequence-masterPage-master의 절차를 나타낸다.Child Object : region-body, region-before, region-after, region-start, resion-end참고 : column-count
•fo:page-sequencePage-master, page-sequence-master를 참조하여 페이지 내용을 나타낸다.
• fo:static-contentHeader/footer와 같이 고정된 내용을 복수 페이지에 반복 적용한다.
• fo:flow하위의 Block-Level FO를 통해 내용을 나타낸다.
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab27
Table FO Tree 체계
• fo:table-column : Column의 width, align 등의 속성을 지정한다.
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab28
List FO 구성
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab29
List FO Tree 체계
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab30
Multi FO 체계
• 기타 FO 정보
http://XML.apache.org/fop
http://www.w3.org/TR/xsl/sliceC.html#prtab1, #prtab2
XSL – FO 트리 체계
Mokpo National University
Distributed Computing Lab31
FO 작업 예
주요 FO 작업 엘리먼트• Fo : root : FO 작업 시작, 최상노드 생성
• Fo : layout-master-set : 마스터 셋 설계
• Fo : simple-page-master : 페이지마스터 사용
• Fo : region-* : 영역 생성
• Fo : page-sequence : 페이지 역속 생성
• Fo : flow : Fo 흐름 생성
• Fo : static-content : 정적 자료 생성
• Fo : block : 블록 레벨 자료 생성
• Fo : inline : 인라인 포맷팅 객체
• Fo : table : 테이블 생성 객체
Mokpo National University
Distributed Computing Lab32
FO 작업 예
FO 적용 예
<kietm>
<chapter>
<chapter-title>employment</chapter-title>
<section1 id="sect-310">
<sec1-title>organization</sec1-title>
<section2 id="sect-320">
<sec2-title>atkhb.</sec2-title>
<para>
<text>the atkhb is task-organized according to the factors of mett-t. as the situation dictates, the aviation brigade commander can organize the atkhb with other assets to form tfs to better perform specific missions.</text> </para>
XML
<?xml version="1.0" encoding="euc-kr"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/TR/WD-xsl
xmlns:fo="http://www.w3.org/1999/XSL/Format"
result-ns="fo"><xsl:template match="/">
<fo:layout-master-set>
<fo:simple-page-master page-master-name="pagemaster-all-pages" height="29.7cm" width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
<fo:region-body margin-top="1cm" margin-bottom="1cm" column-gap="0.25in" border="thin dotted black" padding="6pt" column-count="1"/></fo:simple-page-master></fo:layout-master-set>
XSL
Mokpo National University
Distributed Computing Lab33
FO 작업 예
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master page-master-name="pagemaster-all-pages" height="29.7cm" width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
<fo:region-body margin-top="1cm" margin-bottom="1cm" column-gap="0.25in" border="thin dotted black" padding="6pt" column-count="1" />
</fo:simple-page-master></fo:layout-master-set>
<fo:page-sequence>
<fo:sequence-specification>
XSL-FO
PDF 화일
Mokpo National University
Distributed Computing Lab34
XSL - FO 프로세서
• FOP 현재 제안된 XSL 프로세서, Apache XML 프로젝트에 적용, XML문서를 PDF로 포맷팅
현재 지원하는 Formatting Property
• passiveTeX- 장점
• 개발이 용이하고 안정적이다.
• 폰트, 그래픽 등을 무상으로 사용할 수 있다.
• 수식 처리가 뛰어나다.
• 고품질의 PDF 파일을 생성할 수 있다.
- 단점
• XSL FO가 TeX Page Markup Mode에 맞추어야 한다.
• TeX macro 사용이 모호하고 어렵다.
• 크고 강건해서 다른 프로그램에 내장되기 어렵다.
• PDF 변환에 적용하기에는 시스템이 너무 크다
XSL – FO 프로세서
Mokpo National University
Distributed Computing Lab35
요 약
XSLT
• XML문서의 구조 변환, 템플릿 활용
• XML Stylesheet Language
XPATH
• XML 노드의 위치 경로 선택 및 Predicate Expression 제공
• SQL-like XML 데이터 처리 성능 향상
XSL-FO
• XML문서의 포맷팅 객체 제공, XML2PDF, XML2WML등 다양한 문서 변환
• XSL 의 2단계 활용
연락처E-mail : [email protected]
Tel : 02-536-5093
Homepage : www.kware21.com, www.xmlspecial.com