tags:

views:

174

answers:

3

I have the following xpath that I need to run, but Jquery doesn't support the functions....so how can i actually make this work in jquery ?

$("//*[br]/text()[string-length(normalize-space()) != 0]").each( ....
A: 

John Resig (the author of jQuery) wrote an Xpath Plugin.

Kieran Hall
I believe that just supports the basics, not specific functions like string-length of normalized-space.
meder
+1  A: 

Um - modern jQuery doesn't support XPath. jQuery isn't an Xpath parsing utility.

However, I would recommend you actually translate that into jQuery... perhaps something like

$('*[br]').filter(function() {
    var text = $(this).text(), normalized = $.trim( text );
    return normalized.length>0
});

Though you may need an additional regex replacement of multiple whitespace, I'm not quite sure how that xsl/xpath function works.

meder
Note - I assumed *[br] was any element with a 'br' attribute, I assumed you missed the @ before `br`. Let me know if you actually meant something else instead.
meder
Another sidenote is that some browsers implement the w3 selectors API so if the browser supports that method it will rely on that instead of regular DOm methods. Xpath used to be supported in the old jQuery but I guess it was just too much trouble to maintain both versions and account for all the new functionality.
meder
A: 

Please check the new compatibility Plugin for JQuery:

http://docs.jquery.com/Release%3AjQuery%5F1.2#XPath%5FCompatibility%5FPlugin

But keep one thing in mind: "XPath is a language to traverse nodes in XML document during the transformation (look for XSLT)." :-)

bastianneu