General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Getting all the text children of an element in a script

  • dcramer

    Getting all the text children of an element in a script

    Participants 0
    Replies 1
    Last Activity 12 years, 10 months ago

    Using XMetaL 4.6 (but upgrading soon). I'm writing a script that will iterate through elements that have titles but no id attributes and present the user with a dialog showing the title and a suggested id based on that title (with spaces and weird characters removed). The user can change the id or skip this one. It's all easy enough except getting the text of the title elements. The might have inline markup, but what I want are all the text() children of the title:</p> <p>I've tried the following, but titleText.lenght is always 0.  </p> <p>[code]needIds = ActiveDocument.getNodesByXPath(“//*[not(@id) and ./title]”);<br />titles = ActiveDocument.getNodesByXPath(“//*[not(@id) and ./title]/title”);</p> <p>for (var i=0; i < needIds.length; i++){<br /> needsId = needIds.item(i);<br /> title = titles.item(i);</p> <p> titleText = title.getNodesByXPath(“.//text()”);</p> <p> Application.Alert(“titleText=” + titleText.length);</p> <p> }<br />[/code]</p> <p>Basically, I want to get what I would get using <xsl:value-of select="normalize-space(title)"/> for a given title element. </p> <p>Thanks,<br />David



    Reply to: Getting all the text children of an element in a script

    Ok, I figured it out. Have to use a tree walker:

    [code]function readtree(node) {
    // Read the DOM tree
    // Do if this is a DOMText node
    if (node.nodeType==3) {
    titleText +=;

    // Process this node's children
    if (node.hasChildNodes()) {

    // Continue with this node's siblings
    if (node.nextSibling != null) {


  • You must be logged in to reply to this topic.

Lost Your Password?