试图计算

时间:2016-04-01 22:05:52

标签: c# xml xslt

我正在尝试使用XSLT从XML文件中获取停止次数。

XML:

<allstops>
  <stop number="2504" name="Main &amp; Bainard EB">
    <location>
      <latitude>42.91033567</latitude>
      <longitude>-81.29671483</longitude>
    </location>
    <routes>28</routes>
  </stop>
  <stop number="20" name="Adelaide &amp; Ada NB">
    <location>
      <latitude>42.9742886</latitude>
      <longitude>-81.2252341</longitude>
    </location>
    <routes>16</routes>
  </stop>
  <stop number="22" name="Adelaide &amp; 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

1 个答案:

答案 0 :(得分:0)

你得到了错误的陈述。当前执行number时,将输出当前节点名为/的属性的值。但是您所在的模板匹配 <xsl:value-of select="count(//@number)" /> ,它是文档节点,没有属性。你需要这样做....

 <xsl:value-of select="count(//stop)" />

或者更好的是,这......

<xsl:value-of select="@number"/>

声明stop确实需要替换匹配<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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="count(//stop)"/> 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"> <xsl:sort select="@name" /> </xsl:apply-templates> </table> </body> </html> </xsl:template> <xsl:template match="stop"> <tr> <td> <xsl:value-of select="@number"/> </td> <td> <xsl:value-of select="@name"/> </td> <td> <xsl:value-of select=".//latitude"/> </td> <td> <xsl:value-of select=".//longitude"/> </td> <td> <xsl:value-of select=".//routes"/> </td> </tr> </xsl:template> </xsl:stylesheet> 的模板中的声明。

试试这个XSLT

@authorize
def top_secret():
    ...

# is equivalent to

def top_secret():
    ...
top_secret = authorize(top_secret)
相关问题