[英]How to select a node using XPathNavigator.SelectSingleNode(string xpath)?
[英]No nodes found with XPathNavigator.Select(xpath)
我正在創建C#控制台應用程序以查看站點號,名稱,位置(緯度和經度)和路線號。 並在IE中打開xml文件,我已經創建了XSLT,並且XML文件遵循此Question Xpath和XSLT來將選定的數據顯示為html 。 我添加了Xpath表達式以按名稱查詢stop,如下所示:
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:template match="/">
<xsl:element name="html">
<xsl:element name="body">
<table style="width:720px" border="3">
<tr>
<td>Stop #</td>
<td>Route #</td>
<td>Name</td>
</tr>
<xsl:apply-templates select="//stop[@name=$theStreet]"/>
</table>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="stop">
<tr>
<td>
<xsl:value-of select="@number"/>
</td>
<td>
<xsl:value-of select="routes"/>
</td>
<td>
<xsl:value-of select="@name"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
XML格式
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ltcstops.xslt"?>
<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>
<stop number="24" name="Adelaide & Cheapside St NB">
<location>
<latitude>43.0064704</latitude>
<longitude>-81.2401808</longitude>
</location>
<routes>16</routes>
</stop>
</allstops>
錯誤:找不到預期的“應用模板”聲明!
C#控制台應用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; // XmlDocument class
using System.Xml.XPath; // XPathNavigator class
using System.IO; // Directory class
namespace BusStopApplication
{
class Program
{
private const string XML_FILE = @"Documents\Visual Studio 2012\Projects\C#Project\BusStopApplication\BusStopApplication\ltcstops.xml";
private const string XSLT_FILE_IN = @"Documents\Visual Studio 2012\Projects\C#Project\BusStopApplication\BusStopApplication\ltcstops.xslt";
private const string XSLT_FILE_OUT = "..\\..\\ltcstops_modified.xslt";
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(XSLT_FILE_IN);
// Obtain an XPathNavigator object
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager context = new XmlNamespaceManager(nav.NameTable);
context.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
XPathExpression expr = nav.Compile("//xsl:variable[@name='theStreet']/@select");
expr.SetContext(context);
XPathNodeIterator nodes = nav.Select(expr);
try
{
if (nodes.MoveNext())
{
// Get user's selection
Console.Write("\nEnter street name: ");
string street = Console.ReadLine();
// Build a new 'select' attribute string for the apply-templates element
string selectString = "//stop[@name='" + street.ToUpper() + "']";
// Replace the select attribute
nodes.Current.SetValue(selectString);
// Write new XSLT doc
doc.Save(XSLT_FILE_OUT);
// Display the transformed XML file in Internet Explorer
// NOTE 4: 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() + "\\" + XML_FILE;
proc.Start();
}
else
Console.WriteLine("ERROR: Couldn't find the expected 'apply-templates' declaration!");
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
我添加了Xpath表達式以按名稱查詢stop,如下所示:XSLT
首先,XSLT樣式表不是“ Xpath表達式”(盡管它包含XPath表達式)。
其次,您的XSLT樣式表在第12行引用了一個未聲明的變量$theStreet
。
-
似乎您正在嘗試在運行時將參數傳遞給樣式表。 我不知道如何在C#中執行此操作,但是您的樣式表必須先聲明此類參數,然后才能為其接收值。
看起來您想要做的是在XSLT中動態添加一個變量聲明以創建一個新的XSLT,然后將其保存並用於顯示XML? 建議您采用其他方法解決問題,而不是弄清楚為什么代碼無法正常工作。
可以嘗試向XSLT樣式表中添加一個參數,然后使用XSLCompiledTransform在程序本身中執行轉換,並以此傳遞參數,而不是嘗試通過添加新語句然后在瀏覽器中打開XML來修改XSLT。 ,然后在瀏覽器中打開結果HTML。
首先,您的XSLT中應該有一個xsl:param 。 (直接位於xsl:stylesheet下 )
<xsl:param name="theStreet" />
然后,您將使用XSLCompiledTransform類實際執行到HTML的轉換。 這是代碼示例(在這種情況下,請使用硬編碼的街道名稱)。
using System.Diagnostics;
using System.Xml;
using System.Xml.Xsl;
namespace ConsoleApplication1
{
class Program
{
private const string XML_FILE = @"Documents\ltcstops.xml";
private const string XSLT_FILE_IN = @"Documents\ltcstops.xslt";
private const string HTML_FILE_OUT = @"Documents\ltcstops.html";
static void Main(string[] args)
{
// Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLT_FILE_IN);
// Create the XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();
// Create a parameter which represents the current date and time.
string streetName = "Adelaide & Ada NB";
xslArg.AddParam("theStreet", "", streetName);
// Transform the file.
using (XmlWriter w = XmlWriter.Create(HTML_FILE_OUT))
{
xslt.Transform(XML_FILE, xslArg, w);
}
Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "iexplore";
proc.StartInfo.Arguments = HTML_FILE_OUT;
proc.Start();
}
}
}
然后打開的HTML應該只顯示所選街道名稱的詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.