-1

When using Xpath in JavaScript is there a way to extract the full content of a result, including tags, as a string?

For example:

<myitems>
    <item>item1</item>
    <item>item2</item>
</myitems>

Can the results of '//myitems' be used to print:

<item>item1</item>
<item>item2</item>
Cameron Kirby
  • 75
  • 1
  • 1
  • 5

2 Answers2

0

Try with RegExp like following:

var str = "<myitems><item>item1</item><item>item2</item></myitems>";

var reg = /<(\w+)>(.*)<\/\1>/;
var ret = reg.exec(str);

console.log(ret[2]); // returns <item>item1</item><item>item2</item>

JSFiddle

nanobash
  • 5,419
  • 7
  • 38
  • 56
0

You asked this question two days ago here:

JavaScript Xpath: return result as string

and failed to respond to the answer. Why are you asking it again?

Community
  • 1
  • 1
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • When I last checked there were two answers (not yours but another) that misunderstood the question so I thought I'd rephrase it in a clearer way. I should have just edited it though, something that didn't occur to me at the time. – Cameron Kirby Mar 23 '14 at 14:37