Xsl实践总结(二)

1、模板的使用
工作中会遇到一些编码表的情况,就是数据库中存储的是数值,但显示的是一些实在意义的汉字。
这种情况我们可以定义一个存储数据的xsl文件,我定义了一个codeTab.xsl

然后把这个引用到Archive.xsl文件中,就是要显示真实数据的文件中
我们用学历举例:
<xsl:include href="codeTab.xsl"/>
<xsl:variable name="oppEducation">
  
<xsl:call-template name="GetDegree">
   
<xsl:with-param name="Degree" select="Archives/Education"/>
  
</xsl:call-template>
 
</xsl:variable>
http://www.cnblogs.com/goody9807
codeTab.xsl
<xsl:template name="GetDegree">
        
<xsl:param name="Degree"/>
        
<xsl:variable name="DegreeMapper">
            
<Item>
                
<Name>高中及以下</Name>
                
<Value>1</Value>
            
</Item>
            
<Item>
                
<Name>大学</Name>
                
<Value>2</Value>
            
</Item>
            
<Item>
                
<Name>硕士</Name>
                
<Value>3</Value>
            
</Item>
            
<Item>
                
<Name>博士</Name>
                
<Value>4</Value>
            
</Item>
            
<Item>
                
<Name>博士后</Name>
                
<Value>5</Value>
            
</Item>
        
</xsl:variable>
                
<xsl:value-of select="exslt:node-set($DegreeMapper)/Item[Value=$Degree]/Name"/>
 </xsl:template>

注意:这里用到了扩展的xslt,所以需要在上面定义xmlns:exslt="http://exslt.org/common" 

<xsl:stylesheet version="1.0" xmlns:BitHelper="BitHelper" xmlns:math="http://exslt.org/math" xmlns:exslt="http://exslt.org/common" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


最后在实际用到的地方
<td>学历:<xsl:value-of select="$oppEducation"/></td>
http://www.cnblogs.com/goody9807

2、xsl的中日期的格式转化

在页首定义的地方需要引用date的名称空间
<xsl:stylesheet version="1.0" xmlns:date="http://exslt.org/dates-and-times" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

在出现日期的地方可以通过掩码的方式转化为你想要的日期格式

<td>出生日期:<xsl:value-of select="date:format-date(Archives/Birthday,'yyyy年MM月dd日')"/></td>

3、xsl中用到一些exslt的Math名称空间下的方法

<xsl:variable name="PowerNumbers">
            
<xsl:for-each select="exslt:node-set($MatchIncomeMapper)/Item/Value">
                
<Value>
                    
<xsl:value-of select="math:power(2,number(.)-1)"/>
                
</Value>
            
</xsl:for-each>
        
</xsl:variable>


具体的exslt的一些说明可以参考:
http://www.exslt.org/

4、如何在xsl拼接字符串
    <xsl:for-each select="exslt:node-set($MatchIncomeMapper)/Item[BitHelper:isSelected(number($MatchIncome),Value)]">
                    
<xsl:if test="position()&gt;1">,</xsl:if>
                    
<xsl:value-of select="Name"/>
                
</xsl:for-each>

position方法是xsl内置的方法,意思是取得每次循环中的位置,上面的循环可以达到拼接字符串的效果
就是比如说你有多个学历:
大学,博士,硕士

5、在xsl中引用Java类中的方法
首先我定义一个BitHelper的类

import java.util.*;
public class BitHelper{
    
public static boolean isSelected(long source,int pos){
        
return (source & (1<<(pos-1)))>0;
    }

    
public static int RandomNumber(int minNumber,int maxNumber){

        Random rnd
=new Random();
        
return rnd.nextInt(maxNumber-minNumber+1)+minNumber;
    }

    
static void main(String[] args){
        BitHelper bh
=new BitHelper();
        System.out.print(bh.isSelected(
8,4));
    }

}

http://www.cnblogs.com/goody9807

<xsl:variable name="RandomNumber" select="BitHelper:RandomNumber(1,count($DestLoveTypeSet))"/>

注意要在页面首行引用名称空间
<xsl:stylesheet version="1.0" xmlns:BitHelper="BitHelper" xmlns:exslt="http://exslt.org/common" xmlns:random="http://exslt.org/random" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

希望了解这方面知识的朋友多交流!

相关文章:Xsl实践总结(一)
原文地址:https://www.cnblogs.com/goody9807/p/856363.html