XSL学习笔记3 XSLT的模板规则<xsl:value-of>和<xsl:for-each>

XSLT模板规则详解
本文介绍了XSLT中的两个核心元素&lt;xsl:value-of&gt;和&lt;xsl:for-each&gt;的使用方法。&lt;xsl:value-of&gt;用于在结果树中创建文本节点,而&lt;xsl:for-each&gt;则用于遍历节点集合并进行排序。通过具体的XML文件和XSLT样式表示例,展示了如何利用这两个元素将XML数据转换为HTML表格。

 

XSL学习笔记3 XSLT的模板规则<xsl:value-of>和<xsl:for-each>
 
1、<xsl:value-of>元素
  
<xsl:value-of>元素是选择当前节点的值,用于在结果树中创建一个文本节点。例如<xsl:value-of select="Name" />就是选择Name节点的值。

select属性是必须的,用于指定要计算的表达式,表达式计算的结果将被转换为一个字符串值。如果字符串为空,那么文本节点将不会被创建。
 
注意,每个xsl文件都需要导入xsl的命名空间, [url]http://www.w3.org/TR/WD-xsl[/url],例如在xml头声明之后加入:<p xmlns:xsl=" [url]http://www.w3.org/TR/WD-xsl[/url]">
 
2、<xsl:for-each>元素
 
  <xsl:for-each>元素逐个(select)选择某条件,应用条件。select属性是必须的,用于指定一个表达式,该表达式计算结果必须是一个节点集。<xsl:for-each>元素的内容是一个模板,对于每一个被选择的节点,实例化该模板。
 
for-each还支持排序,order-by语句,它的语法是以分号(;)分隔、作为排序标准的列表。在列表元素前添加加号(+)表示按此标记的内容以升序排序,添加减号(-)表示逆序排序。作为一种简化的表示就是,排序标准列表就是由select规定的标记的子标记的序列,每个标记之间以(;)分隔。
 
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" ?>
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >

     < xsl:template match ="/" >
         < html >
             < head > </ head >
             < xsl:apply-templates />
         </ html >
     </ xsl:template >
    
     < xsl:template match ="employees" >
         < body >
             < table border ="1" >
                 < xsl:for-each select ="employee" >
                     < tr >
                         < td > < xsl:value-of select ="name" /> </ td >
                         < td > < xsl:value-of select ="age" /> </ td >
                         < td > < xsl:value-of select ="monthly_pay" /> </ td >
                     </ tr >
                 </ xsl:for-each >
             </ table >
         </ body >
     </ xsl:template >
    
</ xsl:stylesheet >
 
通过xslt处理器转换为html结果为:
< html >
< head >
     < META http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
</head>

< body >
< table border ="1" >
     < tr >
         < td >zhangsan </td>
         < td >25 </td>
         < td >1200.00 </td>
     </tr>
     < tr >
         < td >lisi </td>
         < td >28 </td>
         < td >1600.00 </td>
     </tr>
     < tr >
         < td >wangwu </td>
         < td >30 </td>
         < td >3500.00 </td>
     </tr>
</table>
</body>
</html>
itemCode过长会超出格子并且好像没换行,<xsl:template match="TableRow"> <fo:table-row height="5mm"> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="no"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="orgCode"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style" font-size="2.2mm"> <fo:block><xsl:value-of select="requestNo"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="noteType"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="state"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="empNo"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block><xsl:value-of select="empName"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap"><xsl:value-of select="itemCode"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" language="zh"><xsl:value-of select="itemDesc"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" language="zh"><xsl:value-of select="qty"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="holdQty"/></fo:block> </fo:table-cell> </fo:table-row> </xsl:template>
最新发布
08-02
<?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, &#39;pt&#39;)"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat(&#39;#&#39;, 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="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://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, &#39;&#39;)"/></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, &#39;SimSun&#39;)[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, &#39;pt&#39;)"/></xsl:attribute></xsl:if><xsl:if test="w:color/@w:val != &#39;auto&#39;"><xsl:attribute name="color"><xsl:value-of select="concat(&#39;#&#39;, 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 = &#39;center&#39;">center</xsl:when><xsl:when test="@w:val = &#39;right&#39;">end</xsl:when><xsl:when test="@w:val = &#39;both&#39;">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, &#39;pt&#39;)"/></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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值