1.03k likes | 1.29k Views
XSLT 와 XSL. Contents. XSL 이란? XSLT 란? XSL 과 XSLT 비교 응용사례. XSL 이란?. XML 문서 구조를 표현할 수 있는 특징. 표현부분을 생략함으로써 장점 확보 확장성 호환성 … XSL 문서 Presentation 능력을 지닌 언어. ( eXtensible Stylesheet Language). Cont. Cont. 신나는 프로야구!. 두산 베어스 선수. 투수 : 진필중 포수 : 홍성흔 1루수: 우 즈 2루수 : 안경현 3루수 : 김동주
E N D
XSLT와 XSL xml programming
Contents • XSL이란? • XSLT란? • XSL과 XSLT 비교 • 응용사례 xml programming
XSL이란? • XML 문서 • 구조를 표현할 수 있는 특징. • 표현부분을 생략함으로써 장점 확보 • 확장성 • 호환성 • … • XSL 문서 • Presentation 능력을 지닌 언어. • (eXtensible Stylesheet Language) xml programming
Cont. xml programming
Cont. 신나는 프로야구! 두산 베어스 선수 투수 : 진필중 포수 : 홍성흔 1루수: 우 즈 2루수 : 안경현 3루수 : 김동주 유격수 : 김민호 우익수 : 심재학 중견수 : 정수근 좌익수 : 장원진 xml programming
Cont. • baseball.xml과 baseball.fo 비교 • baseball.xml • <title>신나는 프로야구!</title> • baseball.fo • <fo:block text-align="center" font-weight="bold" font-size="30pt" font-family="Helvetica, Arial, sans-serif" space-after="30pt" > 신나는 프로야구! </fo:block> xml programming
직접 XML 문서를 변환 XSLT Processor HTML , PDF… 어떻게 변환시킬 것인가? • Transformation? • 변환… XML 문서 XSL 문서 XSLT 문서 xml programming
XSLT란? • XML to XSL • XML to XML • XML to Other Format… • XML to HTML • XML to WML • XML to RTF • XML to etc… xml programming
각각의 V i ewe r 다른 형태의 XML문서 HTML WML PDF RTF, DOC, HWP … Example XSL 문서 XML 문서 XSLT Processor XSLT 문서 xml programming
XSLT와 XSL 비교 • Transformation? • 변환… • Formatting? • Presentation 능력을 지닌 언어 • HTML, PDF, HWP, DOC, RTF… xml programming
… • Formatting Language!!! • Presentation 능력을 지닌 언어 • XSL, HTML, PDF, HWP, DOC, RTF… • Transformation Language!!! • XML문서를 다른 Formatting Language로 바꾸는 역할을 하는 언어. • XSLT xml programming
Cont. xml programming
XSLT의 응용사례 • Data의 변환 XSLT Processor a.XSL XML b.XSL c.XSL xml programming
XSL model • Template-driven model • XML문서가 반복적인 구조로 이루어져 있을 경우. • 메일 머지나 주소록 출력의 경우에 XSL을 적용하여 사용할 수 있다. • Data-driven model • XML문서가 불규칙적인 data로 이루어져 있을 경우. • Template fragments들은 따로따로 각 section을 처리하기 위해 정의되고 사용됨. xml programming
Getting started with XSL • 간단한 예제 <?xml version=“1.0”?> <?xml-stylesheet type=“text/xsl” href=“hello.xsl”?> <greetingk>Hello world</greeting> <?xml version=“1.0”?> <xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“1.0”> <xsl:template match=“/”> <html><head><title>Greeting</title></head> <body><p>Words of greeting : <br> <b><i><u><xsl:value-of select=“greeting”/></u></i></b> </p></body></html> </xsl:template> <xsl:stylesheet> xml programming
Template(틀?) • template의 개념. xml programming
Template • 스타일시트를 이루는 가장 기본적인 단위 <?xml version=“1.0” ?> <xsl:stylesheet version=“1.0” xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=“/”> <HTML> <BODY> <xsl:for-each select=“/strings/s”> <p><xsl:value-of select=“.”/></p> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet> • 템플리트의 구성 • 템플리트가 적용될 소스 트리 부분 • 결과 트리로 삽입되어지는 부분 xml programming
Cont. • 기본 템플리트 • 도큐먼트 루트에 대응하는 템플리트를 정해놓지 않았다면, XSLT는 미리 주어진 정해진 기본 템플리트를 적용한다. • 기본 템플리트 • <xsl:template match=“*|/”> <xsl:apply-templates/> </xsl:template> • 내장 템플리트 • <xsl:template match=“text()|@*”> <xsl:value-of select=“.”/> </xsl:template> • 결국 기본 템플리트를 적용하면 도큐먼트 안의 모든 요소에 적용되어 모든 값이 출력된다. xml programming
XSLT프로세서 결과물 <html> <head></head> <body> </body> </html> toHTML.xsl <xsl:template match=“/”> <html> <head></head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> … class.xml Document(=/) 강좌 이름=“XML” 강사 이름 학생 xml programming
XSLT프로세서 결과물 <html> <head></head> <body> </body> </html> Apply-templates <h1><p align=“CENTER”> XML </p></h1> <table align=“center” border=“1”> </table> Apply-templates toHTML.xsl <xsl:template match=“/”> … <xsl:apply-templates/> … </xsl:template> <xsl:template match=“강좌”> <h1><p align=“CENTER”> <xsl:value-of select=“@이름”/> </p></h1> <table align=“center” border=“1”> <xsl:apply-templates/> </table> </xsl:template> Lab.xml Document(=/) 강좌 이름=“XML” 강사 이름 학생 xml programming
XPath • XML 도큐먼트의 다른 부분을 가리키는 유연한 방법 • 지역 경로 • XPath의 함수들 xml programming
지역 경로 • 도큐먼트(루트) • <xsl:template match=“/”> • 엘레먼트 지정 • <xsl:template match=“/학생”> • <xsl:template match=“학생”> • 경로표현 • <xsl:template match=“/학생/이름”> • <xsl:template match=“학생/이름”> xml programming
Cont. • . : self node • .. : parent node • 재귀적 내림 연산자 • // : 어느 곳에 있든지 상관없이 이름만으로 경로 지정 • <xsl:template match=“//이름”> xml programming
Cont. • [ ]를 이용한 필터링 • <xsl:template match = “학생[주소]”> • <xsl:template match = “강사[@담당과목]”> • <xsl:template match = “학생[@성별=‘남자’]”> • <xsl:template match = “학생[주소=‘경기도’]”> • <xsl:template match = “이름 [.=‘이몽룡’]”> • /학생[@성별=‘남자’]/이름[. = ‘이몽룡’] xml programming
Cont. • 와일드 카드(*) • <xsl:template match=“*”> • <xsl:template match=“@*”> • <xsl:for-each select=“*”> xml programming
XPath의 함수들 ※ 보통의 부모/자식 관계나 엘레먼트/어트리뷰트 관계로는 일치하지 않는 노드와 노드 집합을 찾는데 사용할 수 있다. • 노드 함수들 • 위치지정 함수들 • 산술 함수들 • Boolean함수들 • 문자열 함수들 xml programming
노드 함수들 • name() • node() • processing-instruction() • comment() • text() xml programming
Cont. • name() : 엘레먼트의 이름을 반환한다. • <xsl:template match=“name”> <xsl:for-each select=“*”> <p><xsl:value-of select=“name()”/></p> </xsl:for-each> </xsl:template> • node() • 노드 자신을 반환한다. • 쓰지 않는다. xml programming
Cont. • processing-instruction() • <xsl:template match=“processing-instruction()”> <xsl:value-of select=“.”/> </xsl:template> • <xsl:template match=“processing-instruction(‘stylesheet’)”> <xsl:value-of select=“.”/> </xsl:template> • comment() • <xsl:template match=“comment()”> <xsl:value-of select=“.”/> </xsl:template> xml programming
Cont. • text() • <parent> This is some text. <child>And this is some more text</child> </parent> • <xsl:template match=“parent”> <xsl:value-of select=“.”/> </xsl:template> • <xsl:template match=“parent”> <xsl:value-of select=“text()”/> </xsl:template> xml programming
Cont. • <xsl:template match=“parent”> <xsl:value-of select=“.”/> </xsl:template> • 결과: This is some text. And this is some more text • <xsl:template match=“parent”> <xsl:value-of select=“text()”/> </xsl:template> • 결과: This is some text. xml programming
위치지정 함수들 • position() • last() • count() xml programming
Cont. • position() • 노드 집합 안에서 노드의 위치를 도큐먼트 순으로 얻기 위해 사용된다. • 예제 • <nodes> <node>a</node> <node>b</node> <node>c</node> </nodes> • <xsl:template match=“/nodes/node”> • <xsl:template match=“/nodes/node[position()=2]”> • <xsl:template match=“/nodes/node[2]”> xml programming
Cont. • last() • 제일 마지막 노드의 위치를 반환한다. • 제일 마지막 위치의 노드를 반환하는 것이 아니다. • 예제 • <xsl:template match=“/nodes/node[position() = last()]” • count() • 선택된 노드의 자식 노드 숫자를 반환한다. • 예제 • <xsl:value-of select=“count(node)”/> xml programming
산술 함수들 • number() • sum() xml programming
Cont. • number() • Character값을 숫자값으로 변환한다. • 예제 • <element>256</element> • <xsl:if test=“number(@Security)”> • sum() • 선택된 노드들의 산술값을 더해서 반환한다. • 예제 • <xsl:value-of select=“sum(/nodes/node)”/> xml programming
Boolean 함수들 • boolean() • not() • true() • false() xml programming
Cont. • Boolean() • XPath의 표현에 따라 true 또는 false값을 반환한다. • 숫자인 경우 • 0 -> false • 0 이외에 수 -> true • 숫자가 아닌 경우 • NaN • 문자열인 경우 • 길이가 0이면 -> false • 길이가 0보다 길면 -> true • 노드인경우 • 그 노드가 없으면 -> false • 그 노드가 있으면 -> true xml programming
Cont. • 예제 • <person> <name>YS</name> <addr>KOREA</addr> </person> • <xsl:template match=“person”> <xsl:if test=“boolean(name)”> <!--여기서 작업을 처리한다--> </xsl:if> </xsl:template> xml programming
Cont. • not() • 결과 값을 반대로 변환해서 반환한다. • 예제 • not(boolean(name)) • true(), false() • 항상 true값과 false값을 반환한다. xml programming
문자열 함수들 • string() • string-length() • concat() • contains() • starts-with() • substring() • substring-after() • substring-before() • translate() xml programming
Cont. • string() • 문자열로 바꿔서 반환한다. • string-length() • 공백을 포함한 문자열의 개수를 반환한다. • concat() • 두개이상의 문자열을 받아서 하나의 문자열로 결합한다. • concat(‘my name’, ‘‘, ‘is’, ‘‘, ‘YS’) xml programming
Cont. • contains() • 문자열내에 특정 문자열이 포함되었는지를 확인하여 boolean값을 반환한다. • contain(“my name is YS”, “is YS”) • starts-with() • 문자열이 특정 문자열로 시작되는 지를 확인하여 boolean값을 반환한다. • Start(“my name is YS”, “my”) xml programming
Cont. • substring() • 문자열에서 일부 문자열을 반환한다. • Substring(문자열, 가져올문자의시작위치, 가져올문자의갯수) • 예제 • Substring(“my name is YS”, 5) “ame is YS” • Substring(“my name is YS”, 5, 3) “ame” • substring-after() • 문자열에서 처음 일치하는 특정한 문자뒤에 있는 문자열을 반환한다. • 예제 : Substring-after(“my name is YS”, “a”) “me is YS” • substring-before() • 문자열에서 처음 일치하는 특정한 문자앞에 있는 문자열을 반환한다. • 예제 : Substring-before(“my name is YS”, “a”) “my n” xml programming
Cont. • translate() • 문자열을 변환한다. • 예제 • translate(‘QLSN’, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘zyxwvutsrqpomnlkjihgfedcba’) john 반환 • 예제(대소문자 변환) • translate(‘YSJUNG', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ‘ysjung’반환 xml programming
Cont. • 활용예제 • 예제 : 공백을 더하기 기호로 바꿀때 • translate(‘This is string’, ‘‘, ‘+’) • 예제 : XSLT에서 활용사례 • <xsl:value-of select= “Translate(., ‘abcdefghijklmnopqrstuvwxyz’, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’)” /> xml programming
Cont. • Note!!! • 가령 매치되는 문자가 없을 경우에는 원래문자가 반환 • translate(‘KING’, ‘ING’, ‘inG’) ‘KinG’ • 가령 변환대조 문장열의 길이가 다를 경우, 빼버린다. • translate(‘+bat+’, ‘abc+’, ‘ABC’) ‘BAT’ (‘+’는 매칭되는 변환결과가 없다!!!) • 가령 두번째가 세번째보다 길때, 결과에 영향을 주지 않는다. • Translate(‘zero’, ‘eroz’, ‘EROZA’) ‘ZERO’ xml programming
축을 중심으로한 관계 • 13개의 정의된 축 • self 축 • child 축 • descendant 축 • descendant-or-self 축 • parent 축 • ancestor 축 • ancestor-or-self 축 • following-sibling 축 • preceding-sibling 축 • following 축 • preceding 축 • attribute 축 • namespace 축 xml programming
Cont. • self 축 • 현재노드를 가리킨다. • 예제 • self::node() . • child 축 • 현재노드의 자식들이 있는 축을 가리킨다. • 일반적으로 생략해서 사용 • 예제 • child::order ‘order’ • descendant 축 • 현재노드의 자식들과 자식들의 자식들까지 포함한다. • descendant-or-self 축 • 자신을 포함하고 자식들과 자식들의 자식들까지 포함한다. • ‘//’와 같은 의미 xml programming
Cont. • parent 축 • 현재 노드의 부모를 포함한다. • ancestor 축 • 현재 노드의 부모와 부모의 부모를 포함한다. • ancestor-or-self 축 • 자신을 포함하고, 현재 노드의 부모와 부모의 부모를 포함한다. • following-sibling 축 • 현재 노드 뒤에 오는 형제 노드를 포함한다. • preceding-sibling 축 • 현재 노드 앞에 오는 형제 노드를 포함한다. • following 축 • 현재 노드뒤에 오는 형제 노드와 그 노드의 자식까지 포함한다. • preceding 축 • 현재 노드 앞에 오는 형제 노드와 그 노드의 자식까지 포함한다. xml programming