是否可以使用mustache.js映射xml并使用像{{/ root / Customer}}这样的xpath路由?

时间:2015-08-20 21:30:51

标签: mustache template-engine nustache

是否可以使用mustache.js(或任何其他模板引擎)将xml映射到模板并使用xpath路由,如?

Hello {{/root/Customer}} 
You have won {{/root/Prise}} 

有可能使用过滤器和轴完全使用xpaht查询(“Json路径”不可能是什么)。

2 个答案:

答案 0 :(得分:1)

不回答你的问题:我不认为胡子可以开箱即用。

在研究类似的问题时(我没有在任何模板引擎上修复)我可以省下一些分钱:看看jath并可能从this帖子中提取更多灵感

答案 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。

相关问题