dnewcome / jath

Jath is a simple template language for parsing xml using json markup.
MIT License
68 stars 15 forks source link

Using jQuery Instead of XPath #7

Open matthewrobertson opened 12 years ago

matthewrobertson commented 12 years ago

I am really impressed with Jath. I love the way it converts XML to JSON in a declarative way. And I am even more impressed with the fact that it is only ~ 100 lines of code. But after working with if for a little while, I came up with a similar idea:

-roll the whole thing into a jQuery plugin and use jQuery selectors in the templates instead of XPath queries.

I think I stumbled across Jath in the same way a lot of other users did: I wanted to use Backbonejs, but my data was in an XML format (I dunno if you realize but if you google backbone and xml data the first couple hits are all blog posts and stack overflow questions that recommend PATH). Backbone holds jQuery as a dependency so everyone in this situation already has it in their project. And I think most javascript developers, myself included, are more familiar with jQuery than XPath.

Anyways, just an idea I might toy with in my spare time. If you have any thoughts or would like to work with me on it let me know.

dnewcome commented 12 years ago

Thanks!

The only reason I'm not using jQuery is that you can't access the actual contents of an element using the selector alone. I think I'd have to extend the selector language for it to work as-is. One thing that I'm planning on doing eventually though is using jQuery's XPath capability to allow parsing of HTML the same as XML. Right now, at least under Node.js where I'm using libxml2 it's sometimes possible to use HTML mode but it hasn't been consistent for me.

matthewrobertson commented 12 years ago

The issue of not having a way to directly access node content did cross my mind, but I think you can take this from context in the templating. For example this is what I thought of:

var myTempate = {
    foo: ["#foo-node-id"],
    bar: ["#foo a.btn", "href"]
}
var myTemplate = {
   foo: ".some.complex #selector",
   bar: "@attrname .another#complex[attr='selector']"
}

So attrs would be the first word if and only if it starts with a special char, everything else is the selector. You could even make the attr identifying character configurable in the plugin options.

obviously there are some issues with both these ideas but that's not to say it cannot be done...

dnewcome commented 12 years ago

Looks like an awesome idea to me. Sane defaulting to html() would make things nice and clean in a lot of cases.

matthewrobertson commented 12 years ago

I started it here: https://github.com/matthewrobertson/treadstone

It already works in very basic cases (see the tests). If you want to help or offer input I would appreciate it :)

dnewcome commented 12 years ago

Cool. I'll take a look soon!