Java與XSLT讀書筆記(1)
來源:互聯網
上載者:User
筆記 《Java與XSLT》讀書筆記
一,所有的XSLT處理器必須包括四個內建的模版規則,它們的優先順序要低於任何其他規則,所以只要編寫一個新的模版規則來匹配相同的式樣,就可以覆蓋它們。理解內建規則的最好方法就是架設它們總是位於後台,如果沒有找到其他匹配一個節點的規則,就應用這些內建規則。
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*|/" mode="m">
<xsl:apply-templates mode="m"/>
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="processing-instruction()|comment()">
二,XPath
<message>
<header>
<subject>Hello,World</subject>
<date mm="03" dd="01" yy="2002">
<sender>pres@whitehouse.gov</sender>
<recipient>burke_e@ociWeb.com</recipient>
<recipient>burke_e@yahoo.com</recipient>
<recipient>aidan@burke.com</recipient>
</header>
<body>
…
</body>
</message>
如果<header>為上下文節點,那麼child::sbject將選擇<subject>節點,而child::recipient將選擇所有的<recipient>節點集,child::*將選擇<header>的所有子節點。星號(*)字元是一個萬用字元,表示主要節點類型的所有節點。每個軸都有一個主要節點類型,它始終為元素,除非該軸為屬性或命名空間。如果<date>為上下文節點,那麼attribute::yy將選擇yy屬性,而attribute::*將選擇<date>元素的所有屬性。
簡單增加一個謂詞過濾結果節點集,通常會減少結果集的大小。增加額外的謂詞可進行額外的過濾。例如child::recipient[position()=1]最初將從前一個執行個體中選擇所有的<recipient>元素,然後向下過濾(減少)這個列表直到第一個:burke_e@ociWeb.com位置從1開始,而不是0。謂詞可以包含任何XPath運算式,並且變得相當複雜。
如:
<xsl:apply-templates select="presidents/president[position()=3]/name"/>
<xsl:apply-templates select="presidents/president[count(vicePersident)=0]/name"/>
<xsl:apply-templates select="presidents/president[term/@from < 1800]/name"/>
<xsl:apply-templates select="descendant::president[count(vicePresident)>1]/name"/>
<xsl:apply-templates select="presidents/president/name[child::first=’John’]"/>
<xsl: apply-templates
select="presidents/president[(term/@from > 1800) and (term/@from < 1850)]/name">
三,排序
排序可以應用於資料驅動,也可以應用於模板驅動方法。在任一種情況中<xsl:sort>均作為子項目添加到其他部分。通過增加幾個連續的<xsl:sort>元素,就可以完成多欄位排序。每種排序可以按照升序和降序進行,用於排序的資料類型可以是"數字"或"文本"。排序預設為升序。
<xsl:sort select="first"/>
<xsl:sort select="last" order="descending"/>
<xsl:sort select="term/@from" order="descending" data-type="number"/>
<xsl:sort select="name/first" data-type="text" case-order="upper-first"/>
四,屬性值模板(AVT)
只要我們需要將一個屬性值視為一個XPath運算式而不是靜態文本,可能就需要使用一個AVT。但是對於標準XSLT元素,例如<xsl:template match="pattern"/>,就不必使用AVT文法。對於非XSLT元素,例如任意HTML標籤,就需要AVT文法。
五,<xsl:attribute-set>
<xsl:element name="body" use-attribute-sets="body-style"></xsl:element>
六,<xsl:if>
<xsl:if test="boolean-expression">
<!--@@@-->
</xsl:if>
<xsl:if test="middle">檢查是否存在一個節點集而不是布爾值
<xsl:if test="@someAttribute"
七
<xsl:choose>
<xsl:when test="party=’Democratic’">
<xsl text>blue</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>red</xsl:text>
</xsl:otherwise>
<xsl:choose>
八,參數和變數
<xsl:variable name="lastPresident" select="president[position() = last()]/name"/>
<xsl:value-of select="$lastPresident"/>
九,命名模板
<xsl:template name="formatSSN">
<xsl:param name="ssn"/>
</xsl:template>
<xsl:call-template name="formatSSN">
<xsl:with-param name="ssn" select="@ssn"/>
</xsl:call-template>
十,遞增變數
<!--- familyTree.xml ->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="familyTree.xslt"?>
<person name="Otto">
<person name="Sandra">
<person name="Jeremy">
<person name="Eliana"/>
</person>
<person name="Eric">
<person name="Aidan"/>
</person>
<person name="Philip">
<person name="Alex"/>
<person name="Andy"/>
</person>
</person>
</person>
<!--- familyTree.xslt ->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!-- processing begins here -->
<xsl:template match="/">
<html>
<body>
<!-- select the top level person -->
<xsl:apply-templates select="person">
<xsl:with-param name="level" select="'0'"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<!-- Output information for a person and recursively select
all children. -->
<xsl:template match="person">
<xsl:param name="level"/>
<!-- indent according to the level -->
<div style="text-indent:{$level}em">
<xsl:value-of select="@name"/>
</div>
<!-- recursively select children, incrementing the level -->
<xsl:apply-templates select="person">
<xsl:with-param name="level" select="$level + 1"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
十一,模板模式
<xsl:template match="name" mode="verbose">
</xsl:template>
十二,<xsl:include>與<xsl:import>
<xsl:include>只能作為頂層元素,即它與<xsl:template>是兄弟關係
<xsl:include href="url-reference"/>
當包含兩個衝突模板規則時,就會出現問題
<xsl:import>必須出現在<xsl:stylesheet>的任何其他頂層元素之前
十三,格式化文本和數值
string format-number(number, string, string?)
美元$#.##0.00 #和0是數字預留位置並嚴格符合java.text.DecimalFromat指定的行為
0%輸出一個百分數