XSL学习笔记2 XSLT的模板规则<xsl:template>和<xsl:apply-templates>

本文深入探讨XSLT中的模板规则,包括&lt;xsl:template&gt;和&lt;xsl:apply-templates&gt;的使用方法及属性设置,通过实例说明如何通过模式匹配实现XML文档的转换。

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

XSL学习笔记2  XSLT的模板规则<xsl:template>和<xsl:apply-templates>
 
XSL样式表是由一个或多个被称为“模板” 的规则集组成的。每个模板都包含了与每一个指定节点相匹配的应用规则。
 
模板规则包含两个部分:模式(pattern)和模板(template)。
模板用于在源文档中匹配(定位)节点,模板定义节点的处理规则,通过模板的实例化来组成结果树的一部分。
 
当一个模板实例化的时候,它总是相对于当前节点和当前节点列表来实例化。当前节点总是当前节点列表的成员。在XSLT中,大多数操作都是相对于当前节点的。只有很少的指令改变当前节点列表或者当前节点(例如:<xsl:for-each>指令)。
 
当XSLT处理器使用XSL式样表转换XML文档时,处理器将遍历XML文档的树状结构,一次浏览每个节点,并将浏览的节点与式样表中的每个模板规则的模式进行比较。如果处理器找到了与模板规则的模式相匹配的节点,处理器就会输出此规则的模板。模板通常包含了一些元素指令、新的数据,或者从源XML文档中复制的数据。
 
<xsl:template> 元素
 
<xsl:template> 元素定义了用于匹配节点的规则(match,其中"/"匹配整个文档),在apply-template使用
 
语法规则为:
 
<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number"> 
<!-- Content:(<xsl:param>*,template) -->
</xsl:template>
 
  其中:
  name 模板名称
  match Xpath语句,指定条件
  mode模式,例如红,蓝等样式
  priority 指定模板的优先级,为数字。
 
如果每个模板都赋予了优先级,则处理器可以使用这个值来确定哪个模板具有最高优先级。如果没有显式指定优先级,则处理器会为模板计算一个默认值。由处理器指定的默认优先级范围是从 -0.5 到 +0.5。基本上,模式越特殊,其默认优先级就越高。由于范围是从 -0.5 到 +0.5,因此如果显式指定一个模板的优先级为 1,就总会超过默认优先级。
 
当一个节点匹配在 XSLT 模板中建立的多个模式(也称为规则)时,处理器就会按照 XSLT 规范中描述的冲突解决指导原则来确定使用哪一个模式。这些指导原则表明,当发生冲突时,会调用优先级最高的模板。然而,确定模板实际优先级的算法还需要附带解释一下。
 
要确定哪个模板具有最高优先级,处理器首先会消除导入的所有模板(使用 xsl:import 元素);自动导入的模板比经过导入转换的模板优先级低。然后处理器确定其余模板的优先级值。
 
当出现冲突时,XSLT 需要经过大量处理才能确定调用哪个模板。
 
 
Match 属性的作用是使模板和XML与元素相匹配。Match属性也可以为整个XML文档定义模版。Match属性值是一个XPath表达式。(例如:match="/" 定义整个文档)。
 
employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="src/employees44.xsl"?>
<employees>
    <employee sn="E-200402100001">
        <name>zhangsan</name>
        <age>25</age>
        <monthly_pay mode="cash">
            1200.00
        </monthly_pay>
    </employee>
    <employee sn="E-200402100006">
        <name>lisi</name>
        <age>28</age>
        <monthly_pay mode="cash">
            1600.00
        </monthly_pay>
    </employee>
    <employee sn="E-200503220001">
        <name>wangwu</name>
        <age>30</age>
        <monthly_pay mode="credit_card">
            3500.00
        </monthly_pay>
    </employee>
</employees>
 
employee.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <head>
                <title>employees info</title>
            </head>
        </html>
    </xsl:template>
</xsl:stylesheet>
 
这个是生成的html代码:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>employees info</title>
</head>
</html>
 
<xsl:template>元素是用于创建模板的。
 
<?xml version="1.0" encoding="ISO-8859-1"?>.
因为XSL样式表就是XML文档本身,所以它一般都从XML开始声明:<?xml version="1.0" encoding="ISO-8 59-1"?>。
 
<xsl:stylesheet>,把这份文档定义为XSLT样式表文档(连同版本号和XSLT命名空间属性一起定义)。
 
<xsl:template>元素定义了一份模板。Match=”/”属性使模板和XML源文档的根目录相互联结起来。
Match 属性的作用是使模板和XML与元素相匹配。Match属性也可以为整个XML文档定义模版。Match属性值是一个XPath表达式。(例如:match="/" 定义整个文档)。<xsl:template>元素里面的内容定义一些HTML来对结果进行书写。
 
<xsl:apply-templates>元素
 
<xsl:apply-templates>元素用于告诉处理器处理当前节点的所有子节点。在匹配的节点模板中还可以再包含<xsl:apply-templates>元素,从而通知处理器处理该节点的所有子节点,这样依次调用,就可以完成对文档树中所有节点处理。
 
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="employees">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="employee">
        <xsl:apply-templates select="name"/>
    </xsl:template>
    
</xsl:stylesheet>
 
以上XSL文档定义了三个模板规则:
1、<xsl:template match="/">这个模板匹配XML文档的根节点,告诉处理器处理根节点的所有字节点。
 
2、<xsl:template match="employees"> 这个模板匹配employees节点,告诉处理器处理employees节点的所有字节点。
 
3、最后这个模板复杂些:
<xsl:template match="employee">
        <xsl:apply-templates select="name"/>
    </xsl:template>
<xsl:template match="employee">
 
<xsl:template match="employee">这个模板匹配employee节点,告诉处理器处理employee节点的所有字节点。
select="name",select属性告诉处理器只处理employee节点下的name子节点。
由于employees节点下有三个employee节点,所以第三个模板规则将匹配三次。
 
注意:避免死循环。
 
<?xml version=“1.0” encoding=“UTF-8”?> <xsl:stylesheet version=“3.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” xmlns:fo=“http://www.w3.org/1999/XSL/Format” xmlns:w=“http://schemas.openxmlformats.org/wordprocessingml/2006/main” exclude-result-prefixes=“w”> <!--模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <fo:region-body margin="1in"/> </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:r"/> </fo:block> </xsl:template> <!-- 文本格式处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:apply-templates select="w:rPr"/> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 字体样式提取 --> <xsl:template match="w:rPr"> <xsl:if test="w:rFonts/@w:ascii"> <xsl:attribute name="font-family" select="w:rFonts/@w:ascii"/> </xsl:if> <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size" select="concat(w:sz/@w:val * 0.5, 'pt')"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat('#', w:color/@w:val)"/> </xsl:if> </xsl:template> <!-- 加粗处理 --> <xsl:template match="w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:template> <!-- 斜体处理 --> <xsl:template match="w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:template> <!-- 表格处理 --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%"> <xsl:apply-templates select="w:tr"/> </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 black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 段落默认样式 --> <xsl:attribute-set name="paragraph-style"> <xsl:attribute name="space-after">12pt</xsl:attribute> <xsl:attribute name="text-align">left</xsl:attribute> </xsl:attribute-set> </xsl:stylesheet>完善修改这个xslt样式表,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
03-11
<?xml version="1.0"encoding="UTF-8"?><xsl:stylesheet version="3.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"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:root><fo: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)[1]"/><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"><xsl:value-of select="concat(w:sz/@w:val * 0.5, 'pt')"/></xsl:attribute></xsl:if><xsl:if test="w:color/@w:val != 'auto'"><xsl:attribute name="color"><xsl:value-of select="concat('#', w:color/@w:val)"/></xsl:attribute></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:otherwise>start</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!--段落缩进处理--><xsl:template match="w:ind"><xsl:attribute name="text-indent"><xsl:value-of select="concat(@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>检查这个xlst样式表的问题,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
最新发布
03-12
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="w r"> <xsl:output method="xml" indent="yes"/> <!--模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="2cm" margin-right="2cm"> <fo:region-body margin-top="1cm" margin-bottom="1cm"/> <fo:region-before extent="1cm"/> <fo:region-after extent="1cm"/> </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:body"> <xsl:apply-templates select="*"/> </xsl:template> <!-- 段落处理 --> <xsl:template match="w:p"> <fo:block font-size="12pt" line-height="1.5" space-after="12pt"> <xsl:apply-templates select="w:r"/> </fo:block> </xsl:template> <!-- 文本运行处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:if test="w:rPr/w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:if> <xsl:if test="w:rPr/w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:if> <xsl:value-of select="w:t"/> </fo:inline> </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}dxa"/> </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 black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 列表处理 --> <xsl:template match="w:numPr"> <fo:list-block> <xsl:apply-templates select="w:numId"/> </fo:list-block> </xsl:template> </xsl:stylesheet>在这个基础上添加一下对中文字体的支持
03-09
<?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、付费专栏及课程。

余额充值