天天看點

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>寤堕?挎?″??lt;/storageName>

??? <SumQuantity>27</SumQuantity>

??? <SumMY>749.00</SumMY>

??? <SumSY>749.00</SumSY>

??? <rangename>??绾?lt;/rangename>

??? <getname>寤堕?挎?″??2007-07??-????瀛???????瀛???????瀛???lt;/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>

涓???娴?璇?????浠?est.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");???????? //浣跨??path蹇???璇誨??xml

??????????? XslCompiledTransform transf = new XslCompiledTransform(true);? //浣跨??slt杞???ml瀵矽薄

??????????? 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>