360 likes | 655 Views
자바 개발자를 위한 XML. Chap7. XSLT (XML Stylesheet Language Transformations) 김만수. CHAPTHER 7. XSLT (XML Stylesheet Language Transformations) XSLT 소개 XSLT 원소들. 7.1 XSLT 소개. XSL(XML Stylesheet Language) CSS 와 더불어 XML 문서의 스타일을 정의하기 위한 언어 XSL 구성 XSLT(XSL Transformations)
E N D
자바 개발자를 위한 XML Chap7. XSLT(XML Stylesheet Language Transformations) 김만수
CHAPTHER 7 • XSLT(XML Stylesheet Language Transformations) • XSLT 소개 • XSLT 원소들
7.1 XSLT 소개 • XSL(XML Stylesheet Language) • CSS와 더불어 XML 문서의 스타일을 정의하기 위한 언어 • XSL 구성 • XSLT(XSL Transformations) • XSL-FO(XSL Formatting Objects) • XML의 문법을 준수한 XML 문서 • XSLT • XML 문서를 다른 형태의 XML 문서나 다른 형태의 문서(예:HTML, 텍스트)로 변환 가능 • XSLT + XSL-FO • XML문서를 PDF와 RTF 등의 문서로 변환 가능
7.1 XSLT 소개 XSLT Style Sheet WML VoiceXML XSLT처리기 HTML 문서 XML 문서 XML 문서 DB RTF XSLT처리기 PDF XSLT+FOStyle Sheet
7.1 XSLT 소개 • XSLT • XML 문서를 변환하기 위해서 변환 규칙들을 기술 • 변환 규칙 템플릿(Template) • Template • <template> element로 기술 • <template>의 내용은 문서를 변환하기 위한 명령어와 내용들 • Math 속성을 가짐 • 규칙이 적용될 XML 원소를 찾기 위해 기술 • 값으로 XPath를 사용 • XSLT 문서의 최상위 element는 <stylesheet> • Namespace : http://www.w3.org/1999/XSL/Transform
7.1 XSLT 소개 • 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> Template 매칭 규칙 처리 내용
3. Namespace 4. 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/>를 출력
1 1 2 3 1.1 2 1.1 5 4 3 6
7.1 XSLT 소개 • Template 적용 순서 /
7.1 XSLT 소개 • XSLT 실행 방법 • Java2 v1.4 환경에서 • Batch 파일 작성 java org.apache.xalan.xslt.Process memo.xml memo.xsl memo.html 결과 파일 XSL 파일(Style Sheet) XML 파일(데이터) XSLT 처리 클래스 xslt.bat @echo off rem Xalan XSLT 프로세서 java org.apache.xalan.xslt.Process -IN %1 -XSL %2 -OUT %3
7.1 XSLT 소개 • XML 문서에 바로 정의 • XSLT 기능을 내장한 애플리케이션에서는 바로 처리 됨 • Internet Explorer 6.0 에서 실행 XML 문서에 xsl 파일 URI 지정
7.2 XSLT 원소들 • 순서 • 기본적인 원소들 • 제어문 형태 원소들 • XML 문서 생성 • 기타 원소들 • XSLT 관련 함수들
7.2.1 기본적인 원소들 • <stylesheet> element • XSLT의 ROOT element • Element내에는 다양한 XSLT 원소들을 포함 • <transform> element를 <stylesheet> element 대신 사용 가능 형식 <xsl:stylesheet id = id extension-element-prefixes = tokens exclude-result-prefixes = tokens version = number> <!-- Content: (xsl:import*, top-level-elements) --> <xsl:stylesheet> Stylesheet에서 사용되는 확장 원소의 접두어를 기술 불필요한 namespace를 제거할필요시 해당 namespace 를 기술
7.2.1 기본적인 원소들 • <stylesheet> element • top-level 원소들 • xsl:import • xsl:include • xsl:strip-space • xsl:preserve-space • xsl:output • xsl:key • xsl:decimal-format • xsl:namespace-alias • xsl:attribute-set • xsl:variable • xsl:param • xsl:template 항상 처음 부분에 위치 해야 함 child element의 순서 무시
7.2.1 기본적인 원소들 • <template> element • XSLT에서 가장 중요한 원소 • XSLT에서 문서를 변환하기 위한 작업을 기술 • 구성 • 주어진 XML 문서 내 원하는 부분을 찾기 위한 패턴 정보 XPath로 기술 • 변환된 문서에 추가할 데이터와 명령어 형식 <!-- category: top-level-element --> <xsl:template match = pattern name = qname priority = number mode = qname> <!-- Content: (xsl:param*, template) --> <xsl:template> template을 어느 원소에 적용할 것인지를 기술 template의 우선 순위를 기술
7.2.1 기본적인 원소들 • <apply-templates> element • child node를 처리하기 위해 명시적으로 기술 • 특정 child node를 선택하려면 select 속성을 기술 형식 <!-- category: instruction --> <xsl:apply-templates select = node-set-expression mode = qname> <!-- Content: (xsl:sort | xsl:with-param)* --> < xsl:apply-templates > 예:author element에 template 적용 예:author element에 template 적용 <xsl:template match=“AuthorList”> 저자목록 <xsl:apply-templates select=“Author”/> </xsl:template> <xsl:template match=“AuthorList”> 저자 성명 <xsl:apply-templates select=“Author/Name”/> </xsl:template>
7.2.1 기본적인 원소들 • <value-of> element • XML 문서에 포함된 원소의 내용을 문자열 형태로 접근 형식 <!-- category: instruction --> <xsl:value-of select = string-expression disable-output-escaping = “yes” | “no” /> 예:현재 노드의 텍스트 데이터를 출력 XML element가 아닌 텍스트 데이터만 찾음 <xsl:template match=“text()”> <xsl:value-of select=“.”/> </xsl:template> 현재의 노드를 선택
7.2.1 기본적인 원소들 • <output> element • XSLT 결과로 생성되는 문서를 customize 형식 <!-- category : top-level-element --> <xsl:output method = “xml” | “html” | “text” | qname-but-not-ncname version = nmtoken encoding = string omit-xml-declaration = “yes” | “no” standalone = “yes” | “no” doctype-public = string cdata-section-element = qnames indent = “yes” | “no” media-type = string/> XML 선언 부분이 결과 문서에 포함될 것인지 여부를 기술 결과 문서의 DTD를 기술 예 <xsl:output method=“html” version=“3.2” encoding=“euc-kr”/>
7.2.2 제어문 형태 원소들 • <for-each> element • select 속성을 통해 선택된 노드에 반복적으로 template를 적용 • 프로그래밍 언어에서 for 문과 유사 형식 <!-- category : instruction --> <xsl:for-each select = node-set-expression> <!-- Content: (xsl:sort*, template) --> </xsl:for-each>
7.2.2 제어문 형태 원소들 • <if> element • 조건적으로 template를 적용시 사용 • test 속성 값이 true일 경우 template이 적용 • test 속성 값 • 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>
7.2.2 제어문 형태 원소들 • <choice>, <when>, <otherwise> element • 여러 가지 경우 중에서 조건을 만족하는 한 원소를 처리 • C Language에서 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:
7.2.2 제어문 형태 원소들 pos 변수를 선언하고, 값을 설정 int pos = position() % 3;
7.2.2 제어문 형태 원소들 • <call-template> element • name 속성값이 지정한 template를 호출 • parameter가 필요할 경우 <with-param> element 사용 형식 : <call-template> 형식 : <with-param> <!-- category : instruction --> <xsl:call-template name = qname> <!-- Content: xsl:with-param* --> </ xsl:call-template> <!-- category : top-level-element --> <xsl:with-param name = qname select = expression> <!-- Content: template --> </ xsl:param> P.7-22 참조
7.2.2 제어문 형태 원소들 • <variable> element • 변수를 지정하기 위해 사용 • 한번 값이 할당되면 변경 안됨 • 변수 이름 앞에 $기호 붙임 형식 <!-- Category: top-level-element --> <!-- Category: instruction --> <xsl:variable name = qname select = expression> <!-- Content: template --> </xsl:variable>
7.2.3 XML 문서 생성 • <element> element : element 생성 • 결과 tree에 새로운 XML 원소를 생성 형식 새로 생성할 element의 이름을 현재 속성의 이름으로 지정 <!-- Category: instruction --><xsl:elementname = { qname } namespace = { uri-reference } use-attribute-sets = qnames> <!-- Content: template --></xsl:element> <item> 에 있는 모든 모든 속성들에 대해서
7.2.3 XML 문서 생성 • <attribute> element : 속성 생성 • element의 속성을 생성 • <attribute-set> element • 여러 개의 속성을 기술 형식 <!-- Category: instruction --><xsl:attributename = { qname } namespace = { uri-reference }> <!-- Content: template --></xsl:attribute> 형식 <!-- Category: top-level-element --><xsl:attribute-setname = qname use-attribute-sets = qnames> <!-- Content: xsl:attribute* --></xsl:attribute-set>
7.2.3 XML 문서 생성 모든 element의 이름을 <item> 속성 이름으로 변경하고, element의 text 노드 값을 생성된 속성의 값으로 설정
7.2.3 XML 문서 생성 • <text> element : 텍스트 생성 • <processing-instruction> element : 처리 명령어 생성 • <comment> element : 주석문 생성 • 복사하기 • 소스 tree의 내용을 그대로 결과 tree에 복사 • <copy> • 현재 node를 복사 • <copy-of> • 현재 node와 그 이하 모든 자식 node를 결과 트리에 복사
7.2.3 기타 원소들 • <include> element • XSLT 파일에서 다른 XSLT 파일을 포함 시킴 • <stylesheet>의 child element로만 사용 • <include> element가 사용된 위치에 지정된 XSLT 파일의 내용이 포함 형식 <!-- Category: top-level-element --><xsl:include href = uri-reference/> table.xsl list.xsl a.xsl b.xsl c.xsl
table.xsl goods3.xsl
7.2.3 기타 원소들 • <import> element • <include>와 비슷 • <include>는 중복 정의 허용 안함 <import>는 중복 허용 • <apply-imports> element • 내부의 정의 내용이 외부 <import> 된 내용을 재 정의 하는 경우 외부 파일의 정의를 사용 • <strip-space> element • XML 문서의 불필요한 공백 문자를 삭제 • <preserve-space> element • XML 문서의 공백 문자를 그대로 두어야 하는 경우
7.2.3 기타 원소들 • <number> element • 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 }/>
7.2.3 기타 원소들 • <sort> element • XML 문서의 내용을 정렬 • <apply-templates> or <for-each> element의 child element로 사용될 수 있음 형식 <xsl:sort select = string-expression lang = { nmtoken } data-type = { "text" | "number" | qname-but-not-ncname } order = { "ascending" | "descending" } case-order = { "upper-first" | "lower-first" }/>
7.2.4 XSLT 관련 함수들 • document() 함수 • 다른 XML 문서의 내용을 XSLT를 이용해서 결과 문서에 포함시킬 수 있는 기능을 제공 • generate-id() 함수 • 문서의 ID 속성을 생성하기 위해서 사용