编辑:我可以看到完成您在评论中描述的唯一方法是使用 LINQ to XML、XPath 变量或定义您自己的current()
函数。XPath 变量和当前函数有点麻烦,因为它们需要编写您自己的类来处理它们,但是您可以使用这些相对简单的泛型类:
public class VariableContext : XsltContext
{
IXPathNavigable CurrentNode;
Dictionary<string, object> Values = new Dictionary<string, object>();
public void SetValue(string name, object value)
{
Values[name] = value;
}
public void SetCurrentNode(IXPathNavigable currentNode)
{
CurrentNode = currentNode;
}
public override int CompareDocument(string baseUri, string nextbaseUri)
{
throw new NotImplementedException();
}
public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
{
return false;
}
public override IXsltContextFunction ResolveFunction(string prefix, string name, System.Xml.XPath.XPathResultType[] ArgTypes)
{
if (name.Equals("current"))
{
return new CurrentFunction(CurrentNode);
}
throw new NotImplementedException();
}
public override IXsltContextVariable ResolveVariable(string prefix, string name)
{
object value;
if (Values.TryGetValue(name, out value))
{
return new ContextVariable(name, value);
}
throw new ApplicationException("Unknown variable: " + name);
}
public override bool Whitespace
{
get { return false; }
}
}
public class ContextVariable : IXsltContextVariable
{
private string m_name;
private object m_parameter;
public ContextVariable(string name, object parameter)
{
m_name = name;
m_parameter = parameter;
IXPathNavigable navigable = m_parameter as IXPathNavigable;
if (navigable != null)
{
m_parameter = navigable.CreateNavigator().Select(".");
}
}
#region IXsltContextVariable Members
public object Evaluate(XsltContext xsltContext)
{
return m_parameter;
}
public bool IsLocal
{
get { return true; }
}
public bool IsParam
{
get { return true; }
}
public XPathResultType VariableType
{
get
{
return XPathResultType.Any;
}
}
#endregion
}
public class CurrentFunction : IXsltContextFunction
{
private XPathNodeIterator CurrentNodeIterator;
internal CurrentFunction(IXPathNavigable currentNode)
{
if (currentNode != null)
{
CurrentNodeIterator = currentNode.CreateNavigator().Select(".");
}
}
public XPathResultType[] ArgTypes
{
get { return new XPathResultType[0]; }
}
public object Invoke(XsltContext xsltContext, object[] args,
XPathNavigator docContext)
{
if (CurrentNodeIterator == null)
{
throw new ApplicationException("Current node is not set.");
}
return CurrentNodeIterator;
}
public int Maxargs
{
get { return 0; }
}
public int Minargs
{
get { return 0; }
}
public XPathResultType ReturnType
{
get { return XPathResultType.NodeSet; }
}
}
一旦你在某处定义了它,你可以执行以下操作:
VariableContext vc = new VariableContext();
XmlNode nodea = doc.SelectSingleNode("Root/Start[1]");
vc.SetValue("a", nodea);
XmlNodeList selected = doc.SelectNodes("/Root/Item[@id = $a/@refId]", vc);
或使用自定义current()
函数:
VariableContext vc2 = new VariableContext();
XmlNode nodea2 = doc.SelectSingleNode("Root/Start[1]");
vc2.SetCurrentNode(nodea2);
XmlNodeList selected2 = doc.SelectNodes("/Root/Item[@id = current()/@refId]", vc2);
PrintNodeList(selected2);
工作理念:http: //ideone.com/7wsQ46