Saturday 15 January 2011

javascript - How to select tags with namespaces in CasperJS? -



javascript - How to select tags with namespaces in CasperJS? -

i'm refactoring rss decided write tests casperjs.

one of elements of rss "atom:link" (")

i tried 3 codes, none works

test.assertexists("//atom:link", "atom:link tag exists."); test.assertexists({ type: 'xpath', path: "//atom:link" }, "atom:link element exists."); //even this... test.assertexists({ type: 'xpath', namespace: "xmlns:atom", path: "//atom:link" }, "atom:link element exists.");

the rss code is:

<?xml version="1.0" encoding="utf-8" ?> <rss version="2.0" xml:base="http://example.org/" xmlns:atom="http://www.w3.org/2005/atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/"> <channel> <title>rss title</title> <description>rss description</description> <link>http://example.org</link> <lastbuilddate>mon, 10 nov 2014 11:37:02 +0000</lastbuilddate> <language>es-es</language> <atom:link rel="self" href="http://example.org/rss/feed.xml"/> <item></item> <item></item> </channel> </rss>

i saw in demo in page http://www.freeformatter.com/xpath-tester.html, foo:singers accesible by:

//foo:singers

but in casperjs seems don't work...

anyone know how select kind of elements namespace?

the function casperjs uses resolve elements xpath document.evaluate:

var xpathresult = document.evaluate( xpathexpression, contextnode, namespaceresolver, resulttype, result );

when source code namespaceresolver null. means casperjs cannot utilize xpaths prefixes. if seek it,

[error] [remote] findall(): invalid selector provided "xpath selector: //atom:link":error: namespace_err: dom exception 14

you have create own method retrieve elements user defined nsresolver.

casper.myxpathexists = function(selector){ homecoming this.evaluate(function(selector){ function nsresolver(prefix) { var ns = { 'atom' : 'http://www.w3.org/2005/atom' }; homecoming ns[prefix] || null; } homecoming !!document.evaluate(selector, document, nsresolver, xpathresult.any_type, null).iteratenext(); // retrieve first element }, selector); }; // , later test.asserttrue(casper.myxpathexists("//atom:link"), "atom:link tag exists.");

javascript xml xpath rss casperjs

No comments:

Post a Comment