zoukankan      html  css  js  c++  java
  • xslt转换xml常用知识(2)

    接上次的文章xslt转换xml常用知识(1),介绍xslt转换xml的基本知识.(备注一下:文章中一些符号别编辑器给替换掉了,

    这个--&#--10;是为了不被编辑器给替换掉,故意这样写的,实践上应该是回车的实体符合,不清楚的可以看看我博客里面

    一篇关于xml实体符号的文章)
    1)xsl:template,xsl:variable,xsl:output,xsl:apply-templates,xsl:with-param,xsl:value-of,xsl:for-each,xsl:sort,

    xsl:if,xsl:choose,xsl:when,xsl:call-template等元素的应用。
    2)在xml中选择遍历,文本的回车转换成<br/>等
    如有兴趣大家可以先参看我前面一篇文章:http://www.cnblogs.com/jackhuclan/archive/2008/08/23/1274058.html
    (备注一下:文章中一些符号别编辑器给替换掉了,所有你想学习一下,还是参考我上一篇文章,下载一下代码。)

    一)文本的回车转换成<br/>

      <xsl:template   name="dds">
        
    <xsl:param   name="des"/>
        
    <xsl:choose>
          
    <xsl:when   test="contains($des,' ')">
            
    <xsl:value-of   select="substring-before($des,'--&#--10;')"   />
            
    <br/>
            
    <xsl:call-template   name="dds">
              
    <xsl:with-param name="des" select="substring-after($des,'--&#--10;')"   />
            
    </xsl:call-template>
          
    </xsl:when>
          
    <xsl:otherwise>
            
    <xsl:value-of select="$des"/>
          
    </xsl:otherwise>
        
    </xsl:choose>
      
    </xsl:template>

    上面这段代码就相当于一个函数,<xsl:template   name="dds">,dds相当于函数名称,<xsl:param   name="des"/>,des参数名称
    怎么调用的呢,和大家平时调用函数一样,在需要的地方,引入函数,设定参数。

    <xsl:call-template name="dds">
     
    <xsl:with-param name="des" select="Row/Description"></xsl:with-param>
    </xsl:call-template>


    二)xml中的元素遍历。
    现在需求这样的(参考我的上一篇文章)。在页面左下侧显示一个JobDetail/Row下面的内容,右侧在显示其他的Row下面的内容。
    (需求是这样要求的,不然就直接排序,然后xsl:for-each就出来了,很简单。可要求将这样,就当挑战一下基本知识)
    并且显示要根据Row/DesciptionType的分类。那么怎么做呢?我的算法如下:
    1)保存第一行Row/DesciptionType中的值为全局变量

    <xsl:variable name="desciptionType" select="/Job/JobDetail/Row[1]/DesciptionType"/>


    2)输出该具有相同DesciptionType的所有行,显示在左下侧

    <xsl:apply-templates select="JobDetail">
      
    <xsl:with-param name="des1" select="$desciptionType"></xsl:with-param>
    </xsl:apply-templates>
    <xsl:template match="JobDetail">
        
    <xsl:param name="des1"></xsl:param>
        
    <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#efefef">
          
    <tr>
            
    <td height="27px" style="padding-left:8px">
              
    <h3 style="color:#f9c011;padding-top:16px;">
                
    <xsl:value-of select="Row[DesciptionType=$des1]/strDesciptionType"/>
              
    </h3>
            
    </td>
          
    </tr>
          
    <xsl:for-each select="Row[DesciptionType=$des1]" >
              
    <tr>
                
    <td height="27px" style="padding-left:15px">
                  ■ 
    <xsl:value-of select="ShortDescription"/>
                
    </td>
              
    </tr>
          
    </xsl:for-each>
        
    </table>
        
    <table border="0" cellpadding="0" cellspacing="0">
          
    <tr>
            
    <td height="10"></td>
          
    </tr>
        
    </table>
      
    </xsl:template>

      2)然后对JobDetail/Row排序,然后再进行循环遍历,如果该行已经显示过则跳过,如果没有显示过则将该行的DesciptionType

    赋值给前面定义的全局变量,然后调用模板显示(相当于一个函数)

    <xsl:for-each select="JobDetail/Row">
      
    <xsl:sort select="DesciptionType" data-type="number" order="ascending"/>
      
    <xsl:if test="DesciptionType != $desciptionType">
     
    <xsl:variable name="desciptionType" select="DesciptionType"></xsl:variable>
     
    <xsl:call-template name="jd">
       
    <xsl:with-param name="des1" select="$desciptionType"></xsl:with-param>
       
    <xsl:with-param name="cnode" select="//JobDetail/Row"></xsl:with-param>
     
    </xsl:call-template>
      
    </xsl:if>
    </xsl:for-each>
    <xsl:template name="jd">
        
    <xsl:param name="des1"></xsl:param>
        
    <xsl:param name="cnode"></xsl:param>
        
    <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#efefef">
          
    <tr>
            
    <td height="27px" style="padding-left:8px">
              
    <h3 style="color:#f9c011;padding-top:16px;">
                
    <xsl:value-of select="$cnode[DesciptionType=$des1]/strDesciptionType"/>
              
    </h3>
            
    </td>
          
    </tr>
          
    <xsl:for-each select="$cnode[DesciptionType=$des1]" >
            
    <tr>
              
    <td height="27px" style="padding-left:15px">
                ■ 
    <xsl:value-of select="ShortDescription"/>
              
    </td>
            
    </tr>
          
    </xsl:for-each>
        
    </table>
        
    <table border="0" cellpadding="0" cellspacing="0">
          
    <tr>
            
    <td height="10"></td>
          
    </tr>
        
    </table>
      
    </xsl:template>

        这里面涉及xsl:template,xsl:variable,xsl:output,xsl:apply-templates,xsl:with-param,xsl:value-of,xsl:for-each,xsl:sort,xsl:if,xsl:choose,xsl:when,xsl:call-template等元素的应用。这里简单说明一下,详解可以参考msdn
    xsl:variable 定义全局变量
    xsl:template 输出模板
    xsl:apply-templates 应用模板,对某个节点应用什么模板,如:<xsl:apply-templates select="JobBase"></xsl:apply-templates>,注意select为节点名
    xsl:call-template  调用模板,可以用函数的观念来理解就好理解了。和xsl:apply-templates还是有区别的,仔细看看我的文章将明白了
    xsl:for-each,xsl:sort 前面是循环,后面是排序
    xsl:if,xsl:choose,xsl:when 用于条件选择语句
    xsl:output 定义xml的输出方式,有xml,html 如:<xsl:output method="xml" indent="yes"/>

  • 相关阅读:
    【Eolinker使用】接口测试--如何解决接口重定向
    ExtJS按钮
    Redis-消费模式
    Redis笔记教程
    C++中this指针的用法
    C — 对C语言的认识
    你还在迷茫什么
    2019-2020-1 20199324《Linux内核原理与分析》第四周作业
    2019-2020-1 20199324《Linux内核原理与分析》第三周作业
    2019-2020-1 20199324《Linux内核原理与分析》第二周作业
  • 原文地址:https://www.cnblogs.com/jackhuclan/p/1275620.html
Copyright © 2011-2022 走看看