XML 报表3

【报表】 XSLT 解析普通XML生成表格型XML(三)

文章分类:综合技术

3. XSLT 解析XML为表格型XML的实例(具体请参考附件)
 
Txt代码 复制代码  收藏代码
  1. 附件内容:   
  2.   1. DepInOutData.xml --- 源数据xml   
  3.   2. Pattern.xsl  --- xslt文件   
  4.   3. DepInOutDataResult.xml ---解析结果   
  5.    

* 感兴趣的人可以去下个Eclipse Xslt插件:Orangevolt XSLT。它可以帮助你使用指定的xsl解析指定的xml,无须写java代码。

  需求:根据下面xml中的各中心/部门/科室的收入支出数据,生成一张电子表格,要求同一个中心或者同一个部门的数据的单元格要能合并
(这个是非常简单的一个需求,我们就从这个简单需求入手,看看需要如何操作达到目的)

  提供的源数据XML:参考附件中的DepInOutData.xml
 
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <Report title="测试XSLT" >  
  4.     <Sections>  
  5.         <Center name="销售中心">  
  6.             <Department name="销售1部">  
  7.                     <Section>  
  8.                             <SectionName value="销售1科"/>  
  9.                             <Input value="100.0"/>  
  10.                             <Output value="10.0"/>           
  11.                     </Section>  
  12.                     <Section>  
  13.                             <SectionName value="销售2科"/>  
  14.                             <Input value="101.0"/>  
  15.                             <Output value="12.0"/>           
  16.                     </Section>  
  17.                     <Section>  
  18.                             <SectionName value="销售3科"/>  
  19.                             <Input value="102.0"/>  
  20.                             <Output value="11.0"/>           
  21.                     </Section>  
  22.             </Department>    
  23.                
  24.             <Department name="销售2部">  
  25.                     <Section>  
  26.                             <SectionName value="销售4科"/>  
  27.                             <Input value="100.0"/>  
  28.                             <Output value="10.0"/>           
  29.                     </Section>  
  30.                     <Section>  
  31.                             <SectionName value="销售5科"/>  
  32.                             <Input value="101.0"/>  
  33.                             <Output value="12.0"/>           
  34.                     </Section>  
  35.                     <Section>  
  36.                             <SectionName value="销售6科"/>  
  37.                             <Input value="102.0"/>  
  38.                             <Output value="11.0"/>           
  39.                     </Section>  
  40.             </Department>            
  41.         </Center>  
  42.            
  43.         <Center name="研发中心">  
  44.             <Department name="研发1部">  
  45.                     <Section>  
  46.                             <SectionName value="研发1科"/>  
  47.                             <Input value="100.0"/>  
  48.                             <Output value="10.0"/>           
  49.                     </Section>  
  50.                     <Section>  
  51.                             <SectionName value="研发2科"/>  
  52.                             <Input value="101.0"/>  
  53.                             <Output value="12.0"/>           
  54.                     </Section>  
  55.                     <Section>  
  56.                             <SectionName value="研发3科"/>  
  57.                             <Input value="102.0"/>  
  58.                             <Output value="11.0"/>           
  59.                     </Section>  
  60.             </Department>    
  61.                
  62.             <Department name="研发2部">  
  63.                     <Section>  
  64.                             <SectionName value="研发4科"/>  
  65.                             <Input value="0.0"/>  
  66.                             <Output value="50.0"/>           
  67.                     </Section>  
  68.                     <Section>  
  69.                             <SectionName value="研发5科"/>  
  70.                             <Input value="0.0"/>  
  71.                             <Output value="52.0"/>           
  72.                     </Section>  
  73.                     <Section>  
  74.                             <SectionName value="研发6科"/>  
  75.                             <Input value="0.0"/>  
  76.                             <Output value="51.0"/>           
  77.                     </Section>  
  78.             </Department>    
  79.                
  80.             <Department name="测试1部">  
  81.                     <Section>  
  82.                             <SectionName value="测试1科"/>  
  83.                             <Input value="0.0"/>  
  84.                             <Output value="10.0"/>           
  85.                     </Section>  
  86.                     <Section>  
  87.                             <SectionName value="测试2科"/>  
  88.                             <Input value="0.0"/>  
  89.                             <Output value="12.0"/>           
  90.                     </Section>  
  91.                     <Section>  
  92.                             <SectionName value="测试3科"/>  
  93.                             <Input value="0.0"/>  
  94.                             <Output value="11.0"/>           
  95.                     </Section>  
  96.             </Department>        
  97.         </Center>  
  98.     </Sections>  
  99. </Report>  


下面我就直接给出关键的XSLT代码(具体请参考附件中的Pattern.xsl):
Xslt代码 复制代码  收藏代码
  1. <xsl:for-each select="Report/Sections">  
  2.                     <!-- Create worksheet dynamically -->  
  3.                     <xsl:element name="Worksheet">  
  4.                         <xsl:attribute name="ss:Name">部门收入支出费用表</xsl:attribute>  
  5.                         <xsl:element name="Table">  
  6.                         <!-- ExpandedColumnCount 是表格的列数,>实际的数据的列数-->  
  7.                             <xsl:attribute name="ss:ExpandedColumnCount">6</xsl:attribute>  
  8.                             <!-- ExpandedRowCount 是表格的行数, >实际数据行数即可-->  
  9.                             <xsl:attribute name="ss:ExpandedRowCount">  
  10.                                 <xsl:value-of select="count(.//Section)+10"/>  
  11.                             </xsl:attribute>  
  12.                             <xsl:attribute name="x:FullColumns">1</xsl:attribute>  
  13.                             <xsl:attribute name="x:FullRows">1</xsl:attribute>  
  14.                             <xsl:attribute name="ss:StyleID">s24</xsl:attribute>  
  15.                             <xsl:attribute name="ss:DefaultColumnWidth">90</xsl:attribute>  
  16.                             <xsl:attribute name="ss:DefaultRowHeight">18</xsl:attribute>  
  17.                             <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  
  18.                             <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  
  19.                             <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  
  20.                             <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  
  21.                             <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  
  22.                             <!-- 表格的Title-->  
  23.                             <Row ss:AutoFitHeight="1" ss:Height="18">  
  24.                                 <Cell ss:MergeAcross="4" ss:StyleID="s121">  
  25.                                     <Data ss:Type="String">  
  26.                                         <xsl:value-of select="/Report/@title"/>  
  27.                                     </Data>  
  28.                                 </Cell>                                  
  29.                             </Row>                           
  30.                             <Row ss:Height="29.25">  
  31.                                 <Cell ss:StyleID="s28">  
  32.                                     <Data ss:Type="String">  
  33.                                         <xsl:value-of select="'总部'"/>  
  34.                                     </Data>  
  35.                                 </Cell>  
  36.                                 <Cell ss:StyleID="s28">  
  37.                                     <Data ss:Type="String">  
  38.                                         <xsl:value-of select="'部门'"/>  
  39.                                     </Data>  
  40.                                 </Cell>  
  41.                                 <Cell ss:StyleID="s28">  
  42.                                     <Data ss:Type="String">  
  43.                                         <xsl:value-of select="'科室'"/>  
  44.                                     </Data>  
  45.                                 </Cell>  
  46.                                 <Cell ss:StyleID="s29">  
  47.                                     <Data ss:Type="String">  
  48.                                         <xsl:value-of select="'收入(万元)'"/>  
  49.                                     </Data>  
  50.                                 </Cell>  
  51.                                 <Cell ss:StyleID="s29">  
  52.                                     <Data ss:Type="String">  
  53.                                         <xsl:value-of select="'支出(万元)'"/>  
  54.                                     </Data>  
  55.                                 </Cell>                                                                              
  56.                             </Row>  
  57.                             <xsl:if test="count(.//Section) > 0">  
  58.                                 <xsl:call-template name="Sections"/>                                     
  59.                             </xsl:if>                            
  60.                         </xsl:element>  
  61.                            
  62.                     </xsl:element>  
  63.                 </xsl:for-each>  
  64.           

  
   附件中(放心,俺的开发机杀毒软件具备,一般都杀完毒才放心发布的)
有解析后的结果:DepInOutDataResult.xml,使用MS Office2003以上版本打开可以查看。

  首先有必要将上面的几个重要的常量解释下
    1. ss:ExpandedColumnCount是定义表格的列数
    测试中我发现这个数值必须 大于实际的列数,例如在本例中,我的实际列数是 5列(中心/部门/科室/收入/支出),那么赋值的时候使用的是 6, 如果使用的是5的话,解析的结果文件使用Office无法打开,提示格式错误信息,不信的同学可以尝试下:
   
Xslt代码 复制代码  收藏代码
  1. <xsl:attribute name="ss:ExpandedColumnCount">6</xsl:attribute>  


    2.  ss:ExpandedRowCount是定义表格的行数,当然肯定要 大于实际的行数,这里我做了个算式: 统计出源数据xml中Section个数+10
Xslt代码 复制代码  收藏代码
  1. <xsl:attribute name="ss:ExpandedRowCount">  
  2.                                 <xsl:value-of select="count(.//Section)+10"/>  
  3.                             </xsl:attribute>  

    3. Column... 标签是定义每一列的长宽等属性,实际列数是5列,那么就定义5个Column标签
  
Xslt代码 复制代码  收藏代码
  1. <Column ss:StyleID="s24" ss:AutoFitWidth="1" ss:Width="90"/>  


    4. Row... 标签是定义每一行的长宽等属性,实际行数是多少,那么就定义多少个Row标签
  
Xslt代码 复制代码  收藏代码
  1. <Row ss:AutoFitHeight="1" ss:Height="18">                        
  2.         </Row>  


    5. Cell... 标签是Row的子标签,用于定义每一行中各单元格的数据及样式等等
  
Xslt代码 复制代码  收藏代码
  1. <Cell ss:StyleID="s28">  
  2.                                     <Data ss:Type="String">  
  3.                                         <xsl:value-of select="'总部'"/>  
  4.                                     </Data>  
  5.                                 </Cell>  


   6. xsl:call-template标签是调用函数的, 具体调用哪一个函数则通过属性 name指定。
      本例中填充各项数据,合并单元格等操作都是在函数“Sections”及其子函数中完成
  
Xslt代码 复制代码  收藏代码
  1. <xsl:call-template name="Sections"/>  


   7. ss:MergeAcross是Cell的属性, 用于单元格的横向合并
      下面的例子表示:合并当前单元格右边的4个单元格(即横向区域总共5个单元格被合并)
     
Xslt代码 复制代码  收藏代码
  1. <Cell ss:MergeAcross="4" ss:StyleID="s121">  


   8. ss:MergeDown是Cell的属性, 用于单元格的纵向合并
      下面的例子表示:合并当前单元格下边的4个单元格(即纵向区域总共5个单元格被合并)
     
Xslt代码 复制代码  收藏代码
  1. <Cell ss:MergeDown="4" ss:StyleID="s121">  


  以上即是关键部分,原本想就后面的函数多解释解释,但想来是没大必要的,不仅仅是简单的缘故,相信大家都是牛**的developer,所以解释的太多反倒不美。所以就此打住,刚接触xslt的同学参考例子多思考思考,多写写就可以了 

  

 

转自:http://woniu1983.iteye.com/blog/700749

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值