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?

2 Answers 2

0

You are searching for:

//person/phoneNumber/text()

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

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.