Re: string value of every element



Phantom wrote:
Simple question that's driving me freakin mad.

Hard to answer without seeing your code -- are you working with XPath, or with the DOM, or with one of the other APIs/tools? Are you sure that whatever being returned doesn't need to be iterated over to retrieve the rest of the content?

In XPath, the string value of the fruits element is normally going to be all text contained within it -- which will include the newlines:

"
apple
banana
pear
"

If you had asked for the string value of /fruits/fruit, you would get "apple", since the string value of a multiple-node result is the string value of the first match. You'd need to iterate through the results to gather the other values, and of course concatenating them into a string would be your responsibility.

If you're using the simple DOM operations, elements don't have a string value; you must navigate the tree and gather the values. The DOM's XPath support, if present in your DOM, has an operation which will do the gather-and-concatenate operation -- but again, it's going to include the newlines.
.



Relevant Pages

  • Re: getAttribute question
    ... oddity with IE that getAttributereturns an empty string if the ... HTML specification, ... appropriate places in the HTML 4 and DOM HTML specifications. ...
    (comp.lang.javascript)
  • Mixed SAX and DOM processing: echoing with occassional changes.
    ... assembling them into a DOM as I hit closing elements. ... public void startElement(final String uri, final String localName, ... final String qName, ... // Pass off to serialization. ...
    (comp.lang.java.programmer)
  • Re: Sencha Touch--Support 2 browsers in just 228K!
    ... this particular script is so awful that anyone who could ... These functions are available on every String object. ... if (!dom) { ... @param o The object with the attributes ...
    (comp.lang.javascript)
  • Re: XSLT Transformation in Javascript
    ... I can get a transformed XML string no problem. ... and then grab the child nodes of the in memory div element I don't think there is a way to get from a string to nodes. ... appended to a child in any DOm compliant browser universally. ...
    (microsoft.public.scripting.jscript)
  • Re: Encodings of javascript
    ... How does javascript know that the string it is given is encoded using ... How does the string get inserted into the HTML with UTF-8 encoding? ... The DOM API uses the DOMString type, ... If the browser is Open Source, you can UTSL to confirm that. ...
    (comp.lang.javascript)