我发现在 Xpath 表达式中使用“数字”函数时,该函数仅使用序列中的第一个节点。问题的一个例子:
using System.Xml;
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(
@"<baseball>
<games>
<game>
<id>1</id>
<stadiumId>7</stadiumId>
<attendance>4999</attendance>
</game>
<game>
<id>2</id>
<stadiumId>5</stadiumId>
<attendance>600</attendance>
</game>
<game>
<id>3</id>
<stadiumId>5</stadiumId>
<attendance>789</attendance>
</game>
</games>
<stadiums>
<stadium><id>5</id><capacity>1000</capacity></stadium>
<stadium><id>7</id><capacity>5000</capacity></stadium>
<stadium><id>9</id><capacity>560</capacity></stadium>
</stadiums>
</baseball>");
printNodes("what stadiums had games occur in them?",
doc.SelectNodes("/baseball/stadiums/stadium[./id/text()=/baseball/games/game/stadiumId/text()]"));
printNodes("what stadiums had games occur in them? this time converting to numbers before doing comparison",
doc.SelectNodes("/baseball/stadiums/stadium[number(./id/text())=number(/baseball/games/game/stadiumId/text())]"));
}//Main
private static void printNodes(string description, XmlNodeList nodeList)
{
Console.WriteLine("-----" + description + "-------");
foreach (XmlNode node in nodeList)
Console.WriteLine(node.OuterXml);
Console.WriteLine("-------------------");
}
}//class
}//namespace
输出是:
-----what stadiums had games occur in them?-------
<stadium><id>5</id><capacity>1000</capacity></stadium>
<stadium><id>7</id><capacity>5000</capacity></stadium>
-------------------
-----what stadiums had games occur in them? this time converting to numbers before doing comparison-------
<stadium><id>7</id><capacity>5000</capacity></stadium>
-------------------
它仅在第二个输出中返回体育场 7 的原因(尽管 Xpath 查询在逻辑上相同)是因为第一个棒球/游戏/游戏节点的 StadiumId 为 7。没有评估其他游戏节点。
这是数字功能的预期功能吗?有没有解决的办法?