xslt转换xml

 1.xml文件
<?xml version="1.0" standalone="yes"?>
<GetBooks>
  <GetBookBill>
    <getbookid>1</getbookid>
    <code>20080618140001</code>
    <clientname>徐彦红</clientname>
    <clienttypename>学生</clienttypename>
    <createmanName>swg</createmanName>
    <actualman>徐彦红</actualman>
    <createtime>2008-06-18</createtime>
    <storageName>延长校区</storageName>
    <SumQuantity>27</SumQuantity>
    <SumMY>749.00</SumMY>
    <SumSY>749.00</SumSY>
    <rangename>班级</rangename>
    <getname>延长校区-2007-07博-材料学院-材料学院-材料学院</getname>
    <Book>
      <bookname>数学分析(下)</bookname>
      <bookid>4429</bookid>
      <getbookid>1</getbookid>
    </Book>
    <Book>
      <bookname>体育与健康</bookname>
      <bookid>4433</bookid>
      <getbookid>1</getbookid>
    </Book>
    <Book>
      <bookname>西方经济学(微观部分)</bookname>
      <bookid>4427</bookid>
      <getbookid>1</getbookid>
    </Book>
    <Book>
      <bookname>中华人民共和国公务员法释义</bookname>
      <bookid>4437</bookid>
      <getbookid>1</getbookid>
    </Book>
    <Student>
      <studentname>何先勇(07820008)</studentname>
      <studentcode>07820008</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>刘宜强(07820006)</studentname>
      <studentcode>07820006</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>司新国(07820005)</studentname>
      <studentcode>07820005</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>谭志海(07820001)</studentname>
      <studentcode>07820001</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>谢豪杰(07820002)</studentname>
      <studentcode>07820002</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>徐彦红(07820004)</studentname>
      <studentcode>07820004</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>张欣(07820003)</studentname>
      <studentcode>07820003</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <Student>
      <studentname>张振强(07820007)</studentname>
      <studentcode>07820007</studentcode>
      <getbookid>1</getbookid>
    </Student>
    <BookStuCount>
      <studentcode>07820004</studentcode>
      <bookid>4427</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820002</studentcode>
      <bookid>4427</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820001</studentcode>
      <bookid>4427</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820003</studentcode>
      <bookid>4427</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820005</studentcode>
      <bookid>4427</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820004</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820002</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820001</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820006</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820007</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820008</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820003</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820005</studentcode>
      <bookid>4429</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820004</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820002</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820006</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820008</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820003</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820005</studentcode>
      <bookid>4433</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820004</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820002</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820001</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820006</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820007</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820008</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820003</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
    <BookStuCount>
      <studentcode>07820005</studentcode>
      <bookid>4437</bookid>
      <quantity>1</quantity>
      <getbookid>1</getbookid>
    </BookStuCount>
  </GetBookBill>
</GetBooks>
二、test2.xslt文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <xsl:template name="SplitDim">
    <xsl:param name="base"/>
    <xsl:param name="count"/>
    <xsl:param name="BookBill"/>
    <xsl:variable name="end" select="$base+2"/>
    <BookGroup>
      <xsl:for-each select="$BookBill/Book[position()&gt;=$base and position()&lt;$end]">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </BookGroup>
    <xsl:if test="$count&gt;=$end">
      <xsl:call-template name="SplitDim">
        <xsl:with-param name="base" select="$end"/>
        <xsl:with-param name="count" select="$count"/>
        <xsl:with-param name="BookBill" select="$BookBill"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <xsl:template name="SplitBookGroup">
    <xsl:param name="groupbase"/>
    <xsl:param name="count"/>
    <xsl:param name="Bookgroup" />
    <xsl:variable name="end" select="$groupbase+6"/>
    <BookPage>
      <xsl:for-each select="$Bookgroup/BookGroup[position()&gt;=$groupbase and position()&lt;$end]">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </BookPage>
    <xsl:if test="$count&gt;=$end">
      <xsl:call-template name="SplitBookGroup">
        <xsl:with-param name="groupbase" select="$groupbase" />
        <xsl:with-param name="count" select="$count"/>
        <xsl:with-param name="Bookgroup" select="$Bookgroup" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <xsl:template match="/">
    <PrintScript>
      <PrinterSetup>
      </PrinterSetup>
      <PageSetup>
        <PageSize name="A4"/>
        <!--MyPage" width="800" height="1000"/-->
        <Margins left="1" top="1" right="1" bottom="1"/>
        <Landscape>true</Landscape>
        <Unit>Millimeter</Unit>
      </PageSetup>
      <Styles>
        <Style name="title">
          <Font name="华文行楷" size="22" bold="true"/>
          <Border bottom="0.2"/>
        </Style>
      </Styles>
      <Pages>
        <xsl:for-each select="/GetBooks/GetBookBill">
          <xsl:variable name="BookBill" select="."/>
          <xsl:variable name="Books">
            <xsl:call-template name="SplitDim">
              <xsl:with-param name="base" select="1"/>       
              <xsl:with-param name="count" select="count($BookBill/Book)"/>
              <xsl:with-param name="BookBill" select="$BookBill"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="BookDetail" select="msxsl:node-set($Books)"/>
          <xsl:variable name="BookPages">
            <xsl:call-template name="SplitBookGroup">
              <xsl:with-param name="groupbase" select="1"/>
              <xsl:with-param name="count" select="count($BookDetail/BookGroup)"/>
              <xsl:with-param name="Bookgroup" select="$BookDetail"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="BookGroups" select="msxsl:node-set($BookPages)" />
          <xsl:for-each select="$BookGroups/BookPage">
            <Page>
              <xsl:variable name="page" select="."/>
              <Grid>
                <Columns>
                  <Column width="60"></Column>
                  <xsl:for-each select="$page/BookGroup">
                    <Column width="80"></Column>
                  </xsl:for-each>
                </Columns>
                <Rows>
                  <Row>
                    <Cell>学生</Cell>
                    <xsl:for-each select="$page/BookGroup">
                      <Cell>
                        <xsl:for-each select ="Book">
                          <xsl:value-of select="bookname"/> 
                        </xsl:for-each>                       
                      </Cell>
                    </xsl:for-each>
                  </Row>
                  <xsl:for-each select="$BookBill/Student">
                    <xsl:variable name="StuId" select="studentcode"/>
                    <Row>
                      <Cell>
                        <xsl:value-of select ="studentname"/>
                      </Cell>
                      <xsl:for-each select ="$page/BookGroup">                       
                        <Cell>
                          <xsl:for-each select="Book">
                            <xsl:variable name="bookid" select="bookid"/>
                            <xsl:variable name ="quantity" select ="$BookBill/BookStuCount[bookid=$bookid and studentcode=$StuId]/quantity"/>
                            <xsl:choose>
                              <xsl:when test ="normalize-space($quantity)">
                                <xsl:value-of select ="$quantity"/>
                              </xsl:when>
                              <xsl:otherwise>
                                <xsl:value-of select ="0"/>
                              </xsl:otherwise>
                            </xsl:choose>
                            <xsl:if test ="position()!=last()">:</xsl:if>
                           </xsl:for-each>
                         
                        </Cell>
                      </xsl:for-each>
                    </Row>
                  </xsl:for-each>
                </Rows>
              </Grid>
            </Page>
          </xsl:for-each>        
        </xsl:for-each>
      </Pages>
    </PrintScript>
  </xsl:template>
</xsl:stylesheet>
三、测试的文件test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
using System.Threading;

public partial class DeTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {         
            XPathDocument myXpath = new XPathDocument("c://1.xml");         //使用Xpath快速读取xml
            XslCompiledTransform transf = new XslCompiledTransform(true);  //使用xslt转换xml对象
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = System.Text.Encoding.UTF8;                  //设置使用的文本编码
            settings.Indent = true;                                 //设置缩进元素
            settings.OmitXmlDeclaration = true;                     //设置是否编写xml声明
                      
            XmlWriter writer = XmlTextWriter.Create("c://2.xml",settings); //生成包含xml数据的流和文件          
            transf.Load("c://test2.xslt");
            transf.Transform(myXpath, null, writer);            //将结果输出到流
            writer.Close();
            StreamReader stream = new StreamReader("c://2.xml");    //读取流
            Response.Charset = "gb2312";
            Response.ContentType = "xml/text";
            Response.Write(stream.ReadToEnd());                 //输出到页面
            Response.Flush();
            if (!Response.IsClientConnected)
            { 
                Response.End();
            }
        }
        catch (System.Threading.ThreadAbortException exx)
        {
           
            Console.Write(exx.Message);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
  
}
四、效果如下2.xml
<PrintScript xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <PrinterSetup />
  <PageSetup>
    <PageSize name="A4" />
    <Margins left="1" top="1" right="1" bottom="1" />
    <Landscape>true</Landscape>
    <Unit>Millimeter</Unit>
  </PageSetup>
  <Styles>
    <Style name="title">
      <Font name="华文行楷" size="22" bold="true" />
      <Border bottom="0.2" />
    </Style>
  </Styles>
  <Pages>
    <Page>
      <Grid>
        <Columns>
          <Column width="60" />
          <Column width="80" />
          <Column width="80" />
        </Columns>
        <Rows>
          <Row>
            <Cell>学生</Cell>
            <Cell>数学分析(下)体育与健康</Cell>
            <Cell>西方经济学(微观部分)中华人民共和国公务员法释义</Cell>
          </Row>
          <Row>
            <Cell>何先勇(07820008)</Cell>
            <Cell>1:1</Cell>
            <Cell>0:1</Cell>
          </Row>
          <Row>
            <Cell>刘宜强(07820006)</Cell>
            <Cell>1:1</Cell>
            <Cell>0:1</Cell>
          </Row>
          <Row>
            <Cell>司新国(07820005)</Cell>
            <Cell>1:1</Cell>
            <Cell>1:1</Cell>
          </Row>
          <Row>
            <Cell>谭志海(07820001)</Cell>
            <Cell>1:0</Cell>
            <Cell>1:1</Cell>
          </Row>
          <Row>
            <Cell>谢豪杰(07820002)</Cell>
            <Cell>1:1</Cell>
            <Cell>1:1</Cell>
          </Row>
          <Row>
            <Cell>徐彦红(07820004)</Cell>
            <Cell>1:1</Cell>
            <Cell>1:1</Cell>
          </Row>
          <Row>
            <Cell>张欣(07820003)</Cell>
            <Cell>1:1</Cell>
            <Cell>1:1</Cell>
          </Row>
          <Row>
            <Cell>张振强(07820007)</Cell>
            <Cell>1:0</Cell>
            <Cell>0:1</Cell>
          </Row>
        </Rows>
      </Grid>
    </Page>
  </Pages>
</PrintScript>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值