Я пытаюсь получить число отсчетов остановок из файла XML с помощью XSLT.Попытка получить кол-во
XML:
<allstops>
<stop number="2504" name="Main & Bainard EB">
<location>
<latitude>42.91033567</latitude>
<longitude>-81.29671483</longitude>
</location>
<routes>28</routes>
</stop>
<stop number="20" name="Adelaide & Ada NB">
<location>
<latitude>42.9742886</latitude>
<longitude>-81.2252341</longitude>
</location>
<routes>16</routes>
</stop>
<stop number="22" name="Adelaide & Central Ave NB">
<location>
<latitude>42.9945666</latitude>
<longitude>-81.2343441</longitude>
</location>
<routes>16</routes>
</stop>
</allstops>
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" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:param name="number" />
<xsl:template match="/">
<html>
<body>
<h1>
<font face="Verdana">
ALL LTC Stops
</font>
</h1>
<h2>
<font face="Verdana">
<xsl:value-of select="@number"/> stops found
</font>
</h2>
<table style="width:720px" border="3">
<tr>
<th>
<font face="Verdana" size="4">Stop #</font>
</th>
<th>
<font face="Verdana" size="4">Stop Name</font>
</th>
<th>
<font face="Verdana" size="4">Latitude</font>
</th>
<th>
<font face="Verdana" size="4">Longitude</font>
</th>
<th>
<font face="Verdana" size="4">Routes</font>
</th>
</tr>
<xsl:apply-templates select ="//stop" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="//stop">
<xsl:element name="tr">
<xsl:element name="td">
<xsl:value-of select="count(@number)"/>
</xsl:element>
<xsl:element name="td">
<xsl:value-of select="@name"/>
</xsl:element>
<xsl:element name="td">
<xsl:value-of select=".//latitude"/>
</xsl:element>
<xsl:element name="td">
<xsl:value-of select=".//longitude"/>
</xsl:element>
<xsl:element name="td">
<xsl:value-of select=".//routes"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
C#:
using System;
using System.Xml.Xsl; // Xslt types
using System.IO; // Directory class
namespace GetLTCStops
{
class Program
{
/*
* The following constants assume all the files are located
* in the solution folder which is three folders up from the
* program's runtime folder.
*/
private const string XML_FILE = @"..\..\..\Ltcstops.xml";
private const string XSLT_FILE = @"..\..\..\Ltcstops.xslt";
private const string XSLT_FILE_TWO = @"..\..\..\Ltcstops1.xslt";
private const string HTML_FILE = @"..\..\..\Ltcstops.html";
static void Main(string[] args)
{
// Display a title
Console.WriteLine("London Transit Comission Bus Stop Report");
Console.WriteLine("----------------------------------------");
Console.WriteLine("\nProvide a street name or partial street name exactly as it appears in the XML file.");
string streetName;
do
{
// Get the name of a character from the usuer
Console.Write("\nEnter Street: ");
streetName = Console.ReadLine().ToUpper().Trim();
if (streetName.Length == 0)
{
// Create a new XslTransform object and load the style sheet
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLT_FILE_TWO);
// Set-up an XsltArgumentList object to pass to the
// XSLT file's 'character' parameter
XsltArgumentList xsltArgList = new XsltArgumentList();
xsltArgList.AddParam("number", "", streetName);
// Execute the transformation
FileStream outFile = File.Create(HTML_FILE);
xslt.Transform(XML_FILE, xsltArgList, outFile);
outFile.Close();
// Display the transformed XML file in Internet Explorer
// The rutime folder used by the Internet Explorer (IE) program is
// different from the one used by our C# program. So we're going to give
// IE the absolute path to the XML file by using the GetCurrentDirectory() method.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "iexplore";
proc.StartInfo.Arguments = Directory.GetCurrentDirectory().ToString() + "\\" + HTML_FILE;
proc.Start();
}
}
while (streetName.Length == 0);
try
{
// Create a new XslTransform object and load the style sheet
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLT_FILE);
// Set-up an XsltArgumentList object to pass to the
// XSLT file's 'character' parameter
XsltArgumentList xsltArgList = new XsltArgumentList();
xsltArgList.AddParam("name", "", streetName);
// Execute the transformation
FileStream outFile = File.Create(HTML_FILE);
xslt.Transform(XML_FILE, xsltArgList, outFile);
outFile.Close();
// Display the transformed XML file in Internet Explorer
// The rutime folder used by the Internet Explorer (IE) program is
// different from the one used by our C# program. So we're going to give
// IE the absolute path to the XML file by using the GetCurrentDirectory() method.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "iexplore";
proc.StartInfo.Arguments = Directory.GetCurrentDirectory().ToString() + "\\" + HTML_FILE;
proc.Start();
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
} // end Main method
} // end class
} // end namespace
C# код, являющийся участником соревнований, когда ничего не напечатал он будет работать второй XSLT файл и вернуть все остановки. Но я не могу заставить все найденные остановки возвращаться в этой строке для файла XSLT.
<xsl:value-of select="@number"/> stops found
Благодаря кучу C @ Тим! Не могу поверить, что это было такое простое решение ... У меня есть еще один быстрый вопрос: как мне сделать сортировку стоп и сделать ее сортировкой по возрастанию? – ryuuman123
'xsl: apply-templates' может использоваться с' xsl: sort' для указания заказа. Я внес поправки в свой ответ, чтобы включить это. –
Еще раз спасибо @Tim C, я получил работу! – ryuuman123