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