1

Is there a way to return an xpath result in JavaScript as string that included all the tags and attributes?

For example in the following xml:

<people xmlns:xul = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
    <person>
        <name first="george" last="bush" />
        <address street="1600 pennsylvania avenue" city="washington" country="usa"/>
        <phoneNumber>202-456-1111</phoneNumber>
    </person>
    <person>
        <name first="tony" last="blair" />
        <address street="10 downing street" city="london" country="uk"/>
        <phoneNumber>020 7925 0918</phoneNumber>
    </person>
</people>

The xpath '//person/phoneNumber' would return:

<phoneNumber>202-456-1111</phoneNumber>

<phoneNumber>020 7925 0918</phoneNumber>

Rather than just the numbers?

Cameron Kirby
  • 75
  • 1
  • 1
  • 5

2 Answers2

0

You are searching for:

//person/phoneNumber/text()

Use text() to select only the text content of an element, but not the element itself.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

XPath itself won't serialize the returned elements as lexical XML. But there may be methods in the DOM interface that do so. See for example

How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?

Community
  • 1
  • 1
Michael Kay
  • 156,231
  • 11
  • 92
  • 164