问个样式转换的问题(两种转换方式的结果不同)

Geniusth Geniusth 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我有三个文件:
results.xml(待转换的源文件):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Test.xslt"?>
<results>
<NS:Clip xmlns:NS="http://myFootballMatch" src="http://Multimedia/FootballMatches/worldcupChileArgentina2.rm" IfIn="false">
<NS:Action>角球</NS:Action>
<NS:Duration>
<NS:StartTime>00:32:54Z</NS:StartTime>
<NS:EndTime>00:33:06Z</NS:EndTime>
</NS:Duration>
<NS:Team>智利</NS:Team>
<NS:Player>7号</NS:Player>
<NS:Remark>球没进</NS:Remark>
</NS:Clip>
<NS:Clip xmlns:NS="http://myFootballMatch" src="http://Multimedia/FootballMatches/worldcupChileArgentina2.rm" IfIn="false">
<NS:Action>角球</NS:Action>
<NS:Duration>
<NS:StartTime>00:56:13Z</NS:StartTime>
<NS:EndTime>00:56:33Z</NS:EndTime>
</NS:Duration>
<NS:Team>阿根廷</NS:Team>
<NS:Player>8号里克尔梅</NS:Player>
<NS:Remark>球没进,又获得一个角球</NS:Remark>
</NS:Clip>
<NS:Clip xmlns:NS="http://myFootballMatch" src="http://Multimedia/FootballMatches/worldcupChileArgentina2.rm" IfIn="false">
<NS:Action>角球</NS:Action>
<NS:Duration>
<NS:StartTime>00:56:34Z</NS:StartTime>
<NS:EndTime>00:56:56Z</NS:EndTime>
</NS:Duration>
<NS:Team>阿根廷</NS:Team>
<NS:Player>15号达莱桑德罗</NS:Player>
<NS:Remark>球没进</NS:Remark>
</NS:Clip>
</results>


test.xslt(样式文件):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:matchs="http://myFootballMatch" exclude-result-prefixes="#default matchs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="results">
<html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>新闻</title>
<link href="news.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="results/matchs:Clip">
<table>
<tbody>
<tr>
<th>Item<xsl:value-of select="position()"/></th>
</tr>
<tr>
<td class="title">
<b>动作:</b>
</td>
<td><xsl:value-of select="matchs:Action"/></td>
</tr>
<tr>
<td class="title">
<b>队名:</b>
</td>
<td><xsl:value-of select="matchs:Team"/></td>
</tr>
<tr>
<td class="title">
<b>队员名:</b>
</td>
<td><xsl:value-of select="matchs:Player"/></td>
</tr>
<tr>
<td class="title">
<b>是否有进球:</b>
</td>
<td>
<xsl:variable name="isGoal" select="@IfIn"/>
<xsl:if test="$isGoal = 'false'">
<xsl:text>否</xsl:text>
</xsl:if>
<xsl:if test="$isGoal = 'true'">
<xsl:text>是</xsl:text>
</xsl:if>
</td>
</tr>
<tr>
<td class="title">
<b>评论:</b>
</td>
<td><xsl:value-of select="matchs:Remark"/></td>
</tr>
</tbody>
</table>
<br />
</xsl:template>
</xsl:stylesheet>


program.cs(用于转换的C#程序源代码):

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

using System.IO;
using System.Xml;
using System.Xml.Query;
public class Sample {

public static void Main() {

// Create the XsltCommand object.
XsltCommand xslt = new XsltCommand();
xslt.OnMessageEvent += new QueryEventHandler(MessageCallBack);

// Compile the stylesheet.
xslt.Compile(@"E:\Test\Test.xslt");

// Transform the file.
xslt.Execute(@"E:\Test\results.xml", @"E:\Test\output.html");
}

// Display any event messages.
public static void MessageCallBack(object sender, QueryEventArgs e) {
Console.WriteLine("Message received: {0}", e.Message);
}
}


...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
Geniusth
XML/XSL 8,906 成员 22,677 内容