学习笔记——XSLT创建结果集

来源:互联网 发布:apache ab 测试报告 编辑:程序博客网 时间:2024/05/01 23:04

XSLT提供了一些元素用于直接创建元素、属性、文本、处理命令和注释等元素。

 

创建元素

<!-- Category: instruction -->
<xsl:element
  name = { qname }
  namespace = { uri-reference }
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:element>

 

name:必填属性,表示元素名,可以带命名空间前缀,用于绑定到namespace属性指定的命名空间;

namespace:指定所创建的元素的命名空间URI;

use-attribute-sets:命名属性集的名字,多个之间以空白为分隔符。

 

 

创建属性

<!-- Category: instruction -->
<xsl:attribute
  name = { qname }
  namespace = { uri-reference }>
  <!-- Content: template -->
</xsl:attribute>

 

 name:必填属性,表示属性名,可以带命名空间前缀,用于绑定到namespace属性指定的命名空间;

namespace:指定所创建的属性的命名空间URI;

 

<attribute../>通常要放在其他父元素中使用,表示为该父元素增加指定的属性。

 

命名属性集

<!-- Category: top-level-element -->
<xsl:attribute-set
  name = qname
  use-attribute-sets = qnames>
  <!-- Content: xsl:attribute* -->
</xsl:attribute-set>

 

 name:必填属性,表示命名属性集的名称;

use-attribute-sets:命名属性集的名字,多个之间以空白为分隔符,该属性用于包含其他的命名属性集。

 

<element.../>、<copy.../>、<attribute-set.../>以及XSLT文档中任何普通元素都可以使用use-attribute-sets属性。

 

示例:

<xsl:template match="chapter/heading">  <fo:block quadding="start" xsl:use-attribute-sets="title-style">    <xsl:apply-templates/>  </fo:block></xsl:template><xsl:attribute-set name="title-style">  <xsl:attribute name="font-size">12pt</xsl:attribute>  <xsl:attribute name="font-weight">bold</xsl:attribute></xsl:attribute-set>


创建文本

<!-- Category: instruction -->
<xsl:text
  disable-output-escaping = "yes" | "no">
  <!-- Content: #PCDATA -->
</xsl:text>

 

disable-output-escaping:指定是否禁用特殊字符(如<或&)转义,默认是no,启动转义,不建议修改,容易造成格式混乱。

 

作用:可以对样式表创建的空白进行一定的控制,常用于让样式单格式变的易读。

 

创建处理指令

<!-- Category: instruction -->
<xsl:processing-instruction
  name = { ncname }>
  <!-- Content: template -->
</xsl:processing-instruction>

 

name:处理指令名称。

 

创建注释

<!-- Category: instruction -->
<xsl:comment>
  <!-- Content: template -->
</xsl:comment>

 

For example, this

<xsl:comment>This file is automatically generated. Do not edit!</xsl:comment>

would create the comment

<!--This file is automatically generated. Do not edit!-->

 

复制

<!-- Category: instruction -->
<xsl:copy
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:copy>

 

<!-- Category: instruction -->
<xsl:copy-of
  select = expression />

 

<copy../>:只复制当前元素。

use-attribute-sets:可选属性。

<copy-of.../>:不仅复制当前元素,还会复制该元素所包含的全部属性和子元素。

select:必需属性,其指定将所有匹配的节点复制到结果文档中。

 

 

输出文本 

<!-- Category: instruction -->
<xsl:value-of
  select = string-expression
  disable-output-escaping = "yes" | "no" />

 

<value-of.../>用于输出XML节点的内容,

其支持的属性:

select:必需属性,指定一个XPath表达式,输出表达式内容对应的字符串;

disable-output-escaping:可选属性,用于是否禁用转义。

 

使用注意事项:

1:当<value-of.../>转换包含子节点元素的节点时,采用深度优先法则。

2:输出属性、注释、处理指令时,直接输出其内容。

 

属性值模板

作用:满足根据XML文档内容动态设置结果文档的属性值的需求。拟补<value-of.../>无法动态设置结果文档里的属性值的不足。

 

格式:将expression两侧添加{},即{expression};

 

注意事项:

1:不能嵌套;

2:不能作为select和match属性的值;

3:不要指定输出表达式的值。直接输出表达式的值,应该使用<value-of.../>。

4:不能作为xmlns:xxxPrefix属性的值。

 

示例:

 

<xsl:variable name="image-dir">/images</xsl:variable><xsl:template match="photograph"><img src="{$image-dir}/{href}" width="{size/@width}"/></xsl:template>

 

With this source

<photograph>  <href>headquarters.jpg</href>  <size width="300"/></photograph> 

 

the result would be

<img src="/images/headquarters.jpg" width="300"/>

 

输出格式化数值和编号

<!-- 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 } />

 

<number.../>功能:1:输出格式化数值;2:编号。

 

各个属性的解释:

level:指定以怎样的方式对XML源文档编号,默认属性值single;

count:指定将匹配的节点放在一起进行编号;

form:指定从那个节点开始编号;

value:输出表达式所对应的值,并自动进行将该值舍入到整数,然后转换成字符串插入结果树;

format:指定序列格式;

lang:指定数值序列所使用的语言,若未指定,将根据系统环境来确定;

letter-value:指定使用不同的字符序列来消除歧义;

grouping-separator:指定十进制数中的分组分隔符;

grouping-size:指定十进制数里的分组长度。

 

level属性的可选属性值:

single:将所有元素分级,同一种元素单独编号;

multiple:将所有元素分级,同一种元素单独编号,并将父元素的编号数值自动添加到子元素编号数值之前;

any:把所有元素放在一起编号。

 

format属性的格式:

11 2 ... 10 11 12 ...0101 02 ... 09 10 11 12 ... 99 100 101AA B C ... Z AA AB AC...aa b c ... z aa ab ac....ii ii iii iv v vi vii viii ix x ...II II III IV V VI VII VIII IX X ...

任何其他格式都使用数值序列。

 

grouping-separator和grouping-size必须同时使用,例如:grouping-separator=","且grouping-size="3",则将生成的数字格式为 1,000,000 。

 

示例:

<xsl:template match="title">  <fo:block>     <xsl:number level="multiple"                 count="chapter|section|subsection"                 format="1.1 "/>     <xsl:apply-templates/>  </fo:block></xsl:template><xsl:template match="appendix//title" priority="1">  <fo:block>     <xsl:number level="multiple"                 count="appendix|section|subsection"                 format="A.1 "/>     <xsl:apply-templates/>  </fo:block>


 

<xsl:template match="note">  <fo:block>     <xsl:number level="any" from="chapter" format="(1) "/>     <xsl:apply-templates/>  </fo:block></xsl:template>


 

<xsl:template match="H4"> <fo:block>   <xsl:number level="any" from="H1" count="H2"/>   <xsl:text>.</xsl:text>   <xsl:number level="any" from="H2" count="H3"/>   <xsl:text>.</xsl:text>   <xsl:number level="any" from="H3" count="H4"/>   <xsl:text> </xsl:text>   <xsl:apply-templates/> </fo:block></xsl:template>


 

原创粉丝点击