zoukankan      html  css  js  c++  java
  • xml xslt transform using c#

    用xslt转换xml的代码:附xml和xslt
                try
                {
                    string sourceDoc = "resource-sample.xml";
                    string xsltDoc = "resource.xslt";

                    XPathDocument myXPathDocument = new XPathDocument(sourceDoc);
                    XslCompiledTransform myXslTransform = new XslCompiledTransform();

                    MemoryStream ms = new MemoryStream();

                    XmlTextWriter writer = new XmlTextWriter(ms, null);
                    myXslTransform.Load(xsltDoc);
                    myXslTransform.Transform(myXPathDocument, null, writer);
                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader stream = new StreamReader(ms);
                    this.txtOutput.Text = stream.ReadToEnd();

                    writer.Close();
                }

                catch (FileNotFoundException filexc)
                {
                    MessageBox.Show("File Not Found!", "File Not Found Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


    /*************xml********************/

    <?xml version="1.0" encoding="UTF-8"?>
    <!--Sample XML file generated by XMLSpy v2007 (http://www.altova.com)-->
    <?xml-stylesheet type="text/xsl" href="resource.xslt"?>
    <IPSData xsi:noNamespaceSchemaLocation="resource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <InfoGuid>InfoGuid</InfoGuid>
        <Title>Title</Title>
        <SubTitle>SubTitle</SubTitle>
        <Author>Author</Author>
        <Source>Source</Source>
        <SourceUrl>SourceUrl</SourceUrl>
        <Keywords>Keywords</Keywords>
        <Description>Description</Description>
        <Content>Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.Many Many Content.</Content>
        <MediaFileName>MediaFileName</MediaFileName>
        <InfoType>InfoType</InfoType>
        <Creator>Creator</Creator>
        <CreatorName>CreatorName</CreatorName>
        <CreateTime>2001-12-17T09:30:47.0Z</CreateTime>
        <GroupID>2147483647</GroupID>
        <GroupName>GroupName</GroupName>
        <LastModifier>LastModifier</LastModifier>
        <LastModifierName>LastModifierName</LastModifierName>
        <LastModifyTime>2001-12-17T09:30:47.0Z</LastModifyTime>
        <Auditor>Auditor</Auditor>
        <AuditorName>AuditorName</AuditorName>
        <AuditTime>2001-12-17T09:30:47.0Z</AuditTime>
        <ShowTime>2001-12-17T09:30:47.0Z</ShowTime>
        <Status>Status</Status>
        <ViewCount>0</ViewCount>
        <SendToUserNames>SendToUserNames</SendToUserNames>
        <SendToGroupNames>SendToGroupNames</SendToGroupNames>
        <IconName>IconName</IconName>
        <PageName>PageName</PageName>
        <TalkGuid>TalkGuid</TalkGuid>
        <PushLog>PushLog</PushLog>
        <AppID>AppID</AppID>
        <CategoryPath>String</CategoryPath>
    </IPSData>



    /************xslt********************/
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output version="1.0" method="html" indent="no" encoding="UTF-8"/>
        <xsl:param name="SV_OutputFormat" select="'HTML'"/>
        <xsl:variable name="XML" select="/"/>
        <xsl:template match="/">
            <html>
                <head>
                    <title/>
                </head>
                <body>
                    <xsl:for-each select="$XML">
                        <xsl:for-each select="IPSData">
                            <br/>
                            <span>
                                <xsl:text>标题:</xsl:text>
                            </span>
                            <xsl:for-each select="Title">
                                <xsl:apply-templates/>
                            </xsl:for-each>
                            <br/>
                            <span>
                                <xsl:text>作者:</xsl:text>
                            </span>
                            <xsl:for-each select="Creator">
                                <xsl:apply-templates/>
                            </xsl:for-each>
                            <br/>
                            <span>
                                <xsl:text>来源:</xsl:text>
                            </span>
                            <xsl:for-each select="Source">
                                <xsl:apply-templates/>
                            </xsl:for-each>
                            <br/>
                            <span>
                                <xsl:text>内容:</xsl:text>
                            </span>
                            <xsl:for-each select="Content">
                                <xsl:apply-templates/>
                            </xsl:for-each>
                            <br/>
                        </xsl:for-each>
                    </xsl:for-each>
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>


  • 相关阅读:
    各类运算符练习
    用if语句把24小时制转换成12小时制
    Android课程---优化ListView列表视图
    Android课程---关于ListView列表视图的学习
    Android课程---时间日期对话框
    Android课程---关于对话框的学习
    Android课程---用进度条改变图片透明度
    Android课程---进度条及菜单的学习
    Android课程---final关键字
    Android课程---日历选择器和时间选择器
  • 原文地址:https://www.cnblogs.com/jinweijie/p/755871.html
Copyright © 2011-2022 走看看