当我用.NET 4编译程序作为目标框架时,我遇到了问题。排序不起作用。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
namespace ExampleSortProblemServer
{
class Program
{
static void Main(string[] args)
{
// argument is the filename
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(args[0]);
XslCompiledTransform xslt = new XslCompiledTransform();
XmlResourceResolver resolver = new XmlResourceResolver(typeof (IReport));
xslt.Load(args[1]);
String foStr = CreateFO(xmlDocument, xslt, resolver);
Console.WriteLine(foStr);
}
public static string CreateFO(XmlNode doc, XslCompiledTransform xslt, XmlResolver resolver)
{
string fo;
using (var stringReader = new StringReader(doc.OuterXml))
{
using (var xmlTextReader = new XmlTextReader(stringReader))
{
using (var stringWriter = new StringWriter())
{
using (var xmlTextWriter = new XmlTextWriter(stringWriter))
{
try
{
xslt.Transform(xmlTextReader, null, xmlTextWriter, resolver);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
stringWriter.Flush();
fo = stringWriter.ToString();
}
}
}
}
return fo;
}
}
}
数据是“Daten.xml”:
<daten>
<employees>
<name>Knight</name>
<name>Cook</name>
<name>Superman</name>
<name>Yoda</name>
<name>Albright</name>
</employees>
</daten>
样式表是“Daten.xsl”:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="daten" select="*[local-name()='daten']"/>
<xsl:apply-templates select="$daten">
<xsl:sort select="normalize-space(*[local-name()='employees']/*[local-name()='name']" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
您可以通过以下方式致电该计划: ExampleSortProblemServer.exe Daten.xml Daten.xsl
预期的顺序应为:
奥尔布赖特库克骑士超人尤达
知道什么是不正确的吗?
答案 0 :(得分:2)
我认为这是因为您尝试排序daten
而不是names
。
例如,这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="*[local-name()='daten']/*[local-name()='employees']/*[local-name()='name']">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="name">
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
输入“Daten.xml”:
<daten>
<employees>
<name>Knight</name>
<name>Cook</name>
<name>Superman</name>
<name>Yoda</name>
<name>Albright</name>
</employees>
</daten>
生成此输出:
Albright Cook Knight Superman Yoda
修改强>
样式表只需要稍加更新即可处理您作为答案添加的XML输入。
此输入:
<daten>
<employees>
<employee>
<name>Knight</name>
<forename>Peter</forename>
</employee>
<employee>
<name>Knight</name>
<forename>Gilbert</forename>
</employee>
<employee>
<name>Cook</name>
<forename>Thomas</forename>
</employee>
<employee>
<name>Cook</name>
<forename>Charles</forename>
</employee>
<employee>
<name>Superman</name>
<forename>Kal-El</forename>
</employee>
<employee>
<name>Yoda</name>
<forename></forename>
</employee>
<employee>
<name>Albright</name>
<forename>Peter</forename>
</employee>
<employee>
<name>Albright</name>
<forename>Charles</forename>
</employee>
<employee>
<name>Albright</name>
<forename>Mark</forename>
</employee>
</employees>
</daten>
使用更新的样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="*[local-name()='daten']/*[local-name()='employees']/*[local-name()='employee']">
<xsl:sort order="ascending" select="name"/>
<xsl:sort order="ascending" select="forename"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="name">
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="forename">
<xsl:apply-templates/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
生成您想要的输出:
Albright Charles
Albright Mark
Albright Peter
Cook Charles
Cook Thomas
Knight Gilbert
Knight Peter
Superman Kal-El
Yoda
答案 1 :(得分:0)
这是一个扩展的xml文件:
<daten>
<employees>
<employee>
<name>Knight</name>
<forename>Peter</forename>
</employee>
<employee>
<name>Knight</name>
<forename>Gilbert</forename>
</employee>
<employee>
<name>Cook</name>
<forename>Thomas</forename>
</employee>
<employee>
<name>Cook</name>
<forename>Charles</forename>
</employee>
<employee>
<name>Superman</name>
<forename>Kal-El</forename>
</employee>
<employee>
<name>Yoda</name>
<forename></forename>
</employee>
<employee>
<name>Albright</name>
<forename>Peter</forename>
</employee>
<employee>
<name>Albright</name>
<forename>Charles</forename>
</employee>
<employee>
<name>Albright</name>
<forename>Mark</forename>
</employee>
</employees>
</daten>
结果应该是: