아래 사이트 내용을 간단하게 정리한것임. http://infohost.nmt.edu/tcc/help/pubs/xslt/index.html


지금 일하는 사이트에서 xml과 xslt를 사용해서 xml db를
구현하는 난감한 상황을 맞이하고 있다.

모든 데이터베이스 테이블의 자료를 xml 파일로 변환해서 ftp 로 다운로드후, 프로그램이 데이터베이스를 안보고 이 xml에서 원하는 데이터를 추출하려는 의도로 보이는데,

문제는 모든 프로그램 내 sql query에 상응하는 xslt 파일을 만들어 줘야 한다는 것이며, 또한 간단한 select 하나도 일일이 xslt 로 구현하자니 여간 성가시고 시간 잡아먹는 일이 아닐뿐더러, 거기다 한술 더떠 distinct, group by, like, join , sub query등의 약간만 복잡한 query가 나오는 경우엔 이걸 다 일일이 xslt 문법을 통해서 구현을 해줘야한다는 것이다.

그리고 만약 아주 복잡한 쿼리를 사용한 경우라면 구현 자체가 불가능 해질수도 있다.

xml은 데이터 교환이나 간단한 설정 파일용도로 사용해야지 .. 그나저나 다시 예전의 기억을 되살려서 xslt 문법 보고 있는 내모습 ㅠㅠㅠ

xml 데이터가 다음과 같을때,

<!DOCTYPE park SYSTEM "trails.dtd">
<park name="Lincoln Natural Forest">
    <trail dist="3400" climb="medium">Canyon Trail</trail>
    <trail climb="easy" dist="1200">Pickle Madden Trail</trail>
</park>

다음과 같이 출력을 하길 원하는 경우

<html>
    <head>
    <title>Local Hiking Trails</title>
    </head>
    <body>
    <ul>
        <li>Canyon Trail: 3400 feet, climb medium</li>
        <li>Pickle Madden Trail: 1200 feet, climb easy</li>
    </ul>
    </body>
</html>

xslt 는 다음과 같다.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<!-- 이 템플릿은 루트 노드(/) 를 처리한다. -->
<xsl:template match="/">
    <!--  xsl: prefix 가 없는 태그는 그대로 출력된다. -->
    <html>
    <head>
        <title>Local Hiking Trails</title>
    </head>
    <body>
        <ul>
        <!-- 모든 <trail> element들을 아래 템플릿을 적용해서 변환한다. -->
        <xsl:apply-templates select="park/trail"/>
        </ul>
    </body>
    </html>
</xsl:template>

<!-- 이 템플릿이 <trail> element들을 처리하기 위해 사용된다. !-->
<xsl:template match="trail">
    <li>
    <!-- <trail> element 안의 텍스트를 출력, XPath expression for the content of a node is "." -->
    <xsl:value-of select="."/>
    <!-- colon 과 공백을 출력 -->
    <xsl:text>: </xsl:text>
    <!-- trail element의 "dist=" attribute 출력: @ 사용!  -->
    <xsl:value-of select="@dist"/>
    <xsl:text> feet, climb </xsl:text>
    <xsl:value-of select="@climb"/>
    </li>
</xsl:template>

</xsl:stylesheet>

text() This function selects all the text children of the context node.

comment() Selects all comments that are children of the context node.

processing-instruction()
Selects all children of the context node that are processing instructions.


4.5. XPath operators
e1[e2] Square brackets enclose a predicate, which specifies an expression e2 that selects nodes from a larger set e1. For example, in the XPath expression “para[@class=’note’]”, the para selects all children of the context node, and then the predicate selects only the children that have an attribute class="note". Another example: "item[1]" would select the first child of the context node.

4.6. XPath functions
http://infohost.nmt.edu/tcc/help/pubs/xslt/xpath-functions-sect.html


7.5. : Create an index to optimize input document access http://infohost.nmt.edu/tcc/help/pubs/xslt/xsl-key.html

The purpose of the element is to define an index on information in the input file, so that the XSLT processor can retrieve that information more efficiently than by searching the entire file. This is admittedly an advanced feature, but can be very useful for larger projects.

You must give each key a name, using the name attribute, that will be used to refer to it later. Two more attributes describe which nodes to index, and what text to use as the index:

match 인덱스로 사용할 노드를 지정. 노드셋 To define which nodes of the input document are indexed, use attribute match=”m”, where m is an XPath expression describing a node set.

use 노드의 어떤것을 인덱스로 사용할것인가. To define what content strings are indexed, set this attribute to an XPath expression that describes some content relative to the nodes described by the match attribute.

For example, suppose you have a document with elements that have a map attribute that you'll be referring to elsewhere. You might set up a key called river-map-key by placing this element inside your stylesheet:

<xsl:key name="river-map-key" match="river" use="@map"/>
river 요소의 map 속성을 인덱스로 사용.

8.1. : Define a template http://infohost.nmt.edu/tcc/help/pubs/xslt/template-elts.html

템플릿의 종류:

match -> match="trail" specifies a template that matches all <trail> elements.

name  -> <xsl:call-template>을 사용해서 호출.

8.5. : Define an argument to be passed into a template

green

8.6. : Pass an argument to a template

green

9.2. : Output the value of an expresssion For example, suppose you have defined a variable named lap-count and it currently has a value of 32. This element would place the text "33" in the output:

<xsl:value-of select="$lap-count+1"/>

9.3. : Output an element river 요소에서 이름이 salinity속성과 같은것을 출력.

9.4. : Output an attribute


10.1. : Iterate over a set of nodes select (required) An XPath expression that specifies the node-set over which you want to iterate.

<xsl:for-each select="daughter|son">...</xsl:for-each>

10.2. : Conditional processing This writes the value of variable head-count to the output, but only if it is an odd number:

<xsl:if test="($head-count mod 2)=1">
  <xsl:value-of select="$head-count"/>
</xsl:if>

10.3. : The multiple-case construct

optional <xsl:otherwise>

<xsl:for-each select="turtle">
  <xsl:choose>
    <xsl:when test="position() = 1">
      <xsl:text>(</xsl:text>
      <xsl:apply-templates select="."/>
      <xsl:text>)</xsl:text>
    </xsl:when>
    <xsl:when test="position() = count()">
      <xsl:text>[</xsl:text>
      <xsl:apply-templates select="."/>
      <xsl:text>]</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>

10.4. : Invoke another template

11.10. : Process nodes in a given order You can use multiple elements to specify multiple sort keys. That is, if there are two elements, the first one specifies the primary key, and the second one the secondary sort key (to be used in comparing items that have the same value for the primary key).

select
    An XPath expression that selects the nodes to be sorted. The default value is the node-set of the parent element.

data-type
    Use a value of "text" to treat the items as text strings, or a value of "number" to treat them as numbers. Default is "text".

order
    Use "asc" for ascending order (this is the default), or "desc" for descending order.

case-order
    Specifies how to compare uppercase and lowercase letters. A value of "upper-first" forces uppercase letters before lowercase ones; the opposite is "lower-first".
    The default is language-dependent.

lang
    Defines the language to be used in sorting. See the definition of the language codes online.


<xsl:for-each select="person">
  <xsl:sort select="surname"/>
  <xsl:sort select="first-name"/>
  <xsl:apply-templates select="."/>
</xsl:for-each>

12.4. generate-id(): Generate a unique identifier
The purpose of this function is to generate a string of characters that uniquely identifies a node. Such values are useful for attributes of type ID.

The function takes one optional argument, a node-set: generate-id(S)

where S is a node-set. If S is not given, the function generates an identifier for the context node. If S is empty, the function returns an empty string. If S contains more than one node, the function operates on the one that occurs first in the document.

!!!!!! 동일한 수행에서는, 특정 노드에 대해서는 항상 같은 값을 생성하는것이 보장된다 !!!! Within a given execution, this function will always produce the same value for a given node.

There is no guarantee that it will produce the same value on a different execution of the stylesheet.


12.5. key(): Refer to an index entry
문서 내부 아무곳에서나 노드집합을 가져올수 있게 해준다. This function is used to retrieve a set of nodes from anywhere in the document, using the index specified elsewhere by an element.

key(keyName, keyValue )

where keyName matches the name attribute of an <xsl:key> element,
and keyValue is a string. The result is a node-set containing all the nodes whose value for that key matches keyValue.
키로 정의한 값이 keyValue 와 일치하는 모든 노드들을 가져온다.

 <xsl:key name="river-map-key" match="river" use="@map"/>

2013-05-16 업데이트 : sqlite를 사용하는 라이브러리를 작성.

https://github.com/jeremyko/ComDBLib