xsl:template/xsl:apply-templates/xsl:value-of/xsl:for-each区别

本文介绍了XSLT中的模板概念及其使用方法,包括如何通过match属性指定模板的应用范围,利用apply-templates进行节点处理,以及value-of和for-each等元素的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板(template)是XSLT中最重要的概念之一。XSLT文件就是由一个一个的模板组成,任何一个XSLT文件至少包含一个模板。模板的概念就象是搭积木;你如果是程序员,也可以将模板看作一个方法,一个类,或者一个模块。它们可以被拼装组合,也可以单独成块,不同的模板控制不同的输出格式。

模板(template)由两部分组成:匹配模式(match pattern)和执行。简单的讲模式定义XML源文档中哪一个节点将被模板处理,执行则定义输出的是什么格式。两部分对应的语法为xsl:template和xsl:apply-templates。


xsl:template的语法是:


<xsl:template

match = pattern

name = qname

priority = number

mode = qname>

<!-- 执行内容 -->

</xsl:template>


xsl:template的作用是定义一个新模板。属性中name,priority,和mode用来区别匹配同一节点的不同模板。它们不是常用的属性。match属性则控制模板的匹配模式(pattern),匹配模式是用来定位XML源文档中哪一个节点被模板处理。一个模板匹配一个节点。我们用一个例子来帮助理解:

假设我们要处理一个包含章节和段落文档。我们用para元素定义段落,用chapter元素定义章节。我们来看看match属性可能的值。下面的语句写法说明模板匹配所有的para元素


<xsl:template match="para">

</xsl:template>


下面的语句写法说明模板匹配所有的para元素和所有的chapter元素:


<xsl:template match="(chapter|para)">

</xsl:template>


下面的语句写法说明模板匹配所有的父节点为chapter元素的para元素:


<xsl:template match="chapter//para">

</xsl:template>


下面的语句写法说明模板匹配根节点:


<xsl:template match="/">

</xsl:template>


apply-templates语法:


<xsl:apply-templates

select = node set-expression

mode = qname>

</xsl:apply-templates>


xsl:apply-templates用来执行那一个节点被模板具体处理。你可以将它理解为程序中调用子函数。select属性用来定义确切的节点名称。xsl:apply-templates总是包含在xsl:template元素中,象这样:


<xsl:template match="/">

<xsl:apply-templates select="para"/>

</xsl:template>


这段代码说明摸板匹配整个文档(根节点),具体执行时处理根节点下所有para元素。


<xsl:template match="para">

<p><xsl:apply-templates/></p>

</xsl:template>


而这一段代码则表示摸板匹配para节点,所有para下的子元素都将被处理。

 

XSL:value-of用来将源文档中元素的文本值写到输出文档中。例如:

有一个个人资料的XML文档:


<?xml version="1.0" encoding="iso-8859-1"?>

<PERSON>

<name>ajie</name>

<age>28</age>

</PERSON>


我如果想在输出文档中显示上面这个XML源文档中的name元素的值,可以这样写XSLT代码:


<xsl:template match="PERSON">

<xsl:value-of select="name"/>

</xsl:template>


执行后,你会看到"ajie"被单独显示出来。其中match="PERSON"定义摸板匹配PERSON节点,xsl:value-of
语法说明需要输出一个节点的值,而select="name"则定义需要被输出的元素为name。看这个过程是不是和数据库里查询一个人的名字很象?

xsl:for-each语法允许你循环处理被选择的节点。例如:有一个含多个个人资料的XML文档:

<?xml version="1.0" encoding="iso-8859-1"?>

<PEOPLE>

<PERSON>

<name>ajie</name>

<age>28</age>

</PERSON>

<PERSON>

<name>tom</name>

<age>24</age>

</PERSON>

<PERSON>

<name>miake</name>

<age>30</age>

</PERSON>

</PEOPLE>

我需要显示所有人的姓名,则可以将XSLT代码写成:

<xsl:template match="PEOPLE">

<xsl:for-each select="child::PERSON">

<xsl:value-of select="name"/>

</ xsl:for-each>

</xsl:template>

<?xml version=“1.0”encoding=“UTF-8”?><xsl:stylesheet version=“3.0”xmlns:xsl=“http:xmlns:fo=“http:xmlns:w=“http:exclude-result-prefixes=“w”><!–增强字体映射–><xsl:variable name=“font-mapping”><font w:name=“宋体"fo:name=“SimSun”/><font w:name=“黑体"fo:name=“SimHei”/><font w:name=“等线"fo:name=“DengXian”/></xsl:variable><!–根模板–><xsl:template match=”/”>fo:rootfo:layout-master-set<fo:simple-page-master master-name=“A4"margin=“1in”><fo:region-body margin-top=“0.5in"margin-bottom=“0.5in”/><fo:region-before extent=“0.5in”/><fo:region-after extent=“0.5in”/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference=“A4”><fo:flow flow-name=“xsl-region-body”><xsl:apply-templates select=”//w:body/*”/></fo:flow></fo:page-sequence></fo:root></xsl:template><!–增强段落处理–><xsl:template match=“w:p”><fo:block xsl:use-attribute-sets=“paragraph-style”><xsl:apply-templates select=“w:pPr/w:jc"mode=“align”/><xsl:apply-templates select=“w:pPr/w:ind”/><xsl:apply-templates select=”.//w:r”/></fo:block></xsl:template><!–文本格式处理增强–><xsl:template match=“w:r”>fo:inline<xsl:apply-templates select=“w:rPr”/><xsl:value-of select=“string-join(w:t, ‘’)”/></fo:inline></xsl:template><!–增强字体处理–><xsl:template match=“w:rPr”><xsl:variable name=“w-font"select=“w:rFonts/@w:ascii | w:rFonts/@w:hAnsi”/><xsl:attribute name=“font-family”><xsl:value-of select=”($font-mapping/font[@w:name = $w-font]/@fo:name, $w-font, ‘SimSun’)[1]“/></xsl:attribute><xsl:if test=“w:sz/@w:val”><xsl:attribute name=“font-size"select=“w:sz/@w:val * 0.5 || ‘pt’”/></xsl:if><xsl:if test=“w:color/@w:val != ‘auto’”><xsl:attribute name=“color"select=”‘#’ || w:color/@w:val”/></xsl:if><xsl:apply-templates select=“w:b | w:i | w:u | w:strike”/></xsl:template><!–下划线删除线处理–><xsl:template match=“w:u”><xsl:attribute name=“text-decoration”>underline</xsl:attribute></xsl:template><xsl:template match=“w:strike”><xsl:attribute name=“text-decoration”>line-through</xsl:attribute></xsl:template><!–表格处理增强–><xsl:template match=“w:tbl”><fo:table table-layout=“fixed"width=“100%”><xsl:for-each select=“w:tblGrid/w:gridCol”><fo:table-column column-width=”{@w:w div 20}pt”/></xsl:for-each>fo:table-body<xsl:apply-templates select=“w:tr”/></fo:table-body></fo:table></xsl:template><xsl:template match=“w:tr”>fo:table-row<xsl:apply-templates select=“w:tc”/></fo:table-row></xsl:template><xsl:template match=“w:tc”><fo:table-cell border=“1pt solid #000"padding=“4pt”>fo:block<xsl:apply-templates select=”.//w:p"/></fo:block></fo:table-cell></xsl:template><!–段落对齐处理–><xsl:template match=“w:jc"mode=“align”><xsl:attribute name=“text-align”>xsl:choose<xsl:when test=”@w:val = ‘center’“>center</xsl:when><xsl:when test=”@w:val = ‘right’“>end</xsl:when><xsl:when test=”@w:val = ‘both’“>justify</xsl:when>xsl:otherwisestart</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!–段落缩进处理–><xsl:template match=“w:ind”><xsl:attribute name=“text-indent”><xsl:value-of select=”@w:firstLine div 20 || ‘pt’"/></xsl:attribute></xsl:template><!–增强段落样式–><xsl:attribute-set name=“paragraph-style”><xsl:attribute name=“space-after”>12pt</xsl:attribute><xsl:attribute name=“line-height”>1.5</xsl:attribute><xsl:attribute name=“text-align”>left</xsl:attribute></xsl:attribute-set></xsl:stylesheet>这个不对,xsl:attribute没有select属性,请给出完整的修改好的结果
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值