是否可以使用mustache.js(或任何其他模板引擎)将xml映射到模板并使用xpath路由,如?
Hello {{/root/Customer}}
You have won {{/root/Prise}}
有可能使用过滤器和轴完全使用xpaht查询(“Json路径”不可能是什么)。
答案 0 :(得分:1)
答案 1 :(得分:1)
我通过实现IDictionary完成了这个技巧(对于nushache)。
public class RenderXpath : IDictionary<string,object>
{
XElement xElement;
public RenderXpath(XElement xElement)
{
this.xElement = xElement;
}
public object this[string key]
{
get
{
string @value = "";
var element = xElement.XPathSelectElement(((key as string)??"").Trim());
if (element.Elements().Count() > 0)
return new RenderXpath(element); // support loops
if (element.Value != null)
return element.Value;
return @value;
}
set
{
throw new NotImplementedException();
}
}
public bool ContainsKey(string key)
{
return true;
}
// all other throws NotImplementedException
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
语法为{{xmlDoc./License/Customer}} 感谢胡子,没有必要逃脱'/'char。