-1

我一直难以理解 XPath。

我想做的是从货币国家代码中提取汇率。

我的带有货币的 XML 在这里: http: //www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml

这是我的查询:

query = xml.XPathSelectElement(**"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"**).Value;

我在 .NET 3.5 和一个 asp webservice 项目中工作,这会产生以下错误:

System.Xml.XPath.XPathException: 'exhangerates/dailyrates/currency[@code='USD']@rate' has an invalid token.
   at MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion)
   at MS.Internal.Xml.XPath.QueryBuilder.Build(String query, Boolean allowVar, Boolean allowKey)
   at MS.Internal.Xml.XPath.QueryBuilder.Build(String query, Boolean& needContext)
   at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
   at System.Xml.XPath.XPathNavigator.Evaluate(String xpath, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.XPathEvaluator.Evaluate[T](XNode node, String expression, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.Extensions.XPathSelectElements(XNode node, String expression, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.Extensions.XPathSelectElement(XNode node, String expression)
   at WebServiceCurrency.Service1.RetrieveExchangeRate(String country) in c:\Users\Peter\Documents\GitHub\HotMess\C#\Webservices\WebServiceCurrency\WebServiceCurrency\Service1.asmx.cs:line 35
4

2 回答 2

0

这有效:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>    
 <xsl:template match="/">

   <xsl:value-of select="//dailyrates/currency[@code='USD']/@rate" />

 </xsl:template>
</xsl:stylesheet>

输出:

566,97

所以你的解决方案应该是

query = xml.XPathSelectElement(**"exhangerates/dailyrates/currency[@code='"+ country +"']/@rate"**).Value;
于 2012-12-18T02:19:29.760 回答
0

您的错误消息试图告诉您您的 XPath 有语法错误。为了

"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"

"exhangerates/dailyrates/currency[@code='"+ country +"']/@rate"

也就是说,在导航到currency节点的步骤和导航到其rate属性的下一步骤之间插入一个斜线。(我看到 PCM 在这个解决方案上击败了我。脱帽致敬。)

于 2012-12-18T02:22:45.680 回答