1

I have an xml file with words and definitions.

<word definition="this is my definition">myWord</word>

When my html page has myWord, I want to change myWord into a link with its title holds the definition of my word.

Basicly its something like this;

myWord turns into

<a href="#" title="this is my definition">myWord </a> 

To link from xml I am using this code. But this code links a several times, even words in definition get linked. So I want to check if it is alread linked, if so, skip.

x=xmlDoc.getElementsByTagName('word');
for (i=0;i' + x.item(i).attributes[0].textContent + ': ' + x.item(i).attributes[1].textContent + '\', this);" onMouseOut="doClear();">' +x[i].childNodes[0].nodeValue + '';
    replacer(text, linked);     

I have a replacer function

function replacer(oldstring, newstring) {
 if(oldstring inside replaced tag)
  {
       //skip replacement
  }

 else
  {
    document.body.innerHTML = document.body.innerHTML.replace(/oldstring/, newstring);
  }
}


I want to skip replace if my oldstring is inside

<span class="replaced">Replaced text</span>

How can I write if section of this code.

I have been struggling over this for days. Please help me! Thank in advance!

testere
  • 91
  • 1
  • 1
  • 5
  • 7
    You probably don't want to be doing this at the string level, because it's very difficult to parse HTML (you can't do it reliably with regular expressions alone, for instance, although many have tried). Instead, I'd insert the text into a DOM element (it can be disconnected and therefore not displayed) and then loop through the text nodes within it, skipping the ones in elements you don't want to process. Relevant ref material: [DOM2 Core](http://www.w3.org/TR/DOM-Level-2-Core/), [DOM2 HTML](http://www.w3.org/TR/DOM-Level-2-HTML/), [DOM3 Core](http://www.w3.org/TR/DOM-Level-3-Core/) – T.J. Crowder Jun 19 '11 at 17:50
  • Can you show me how to do it, please! – testere Jun 19 '11 at 17:51
  • I'm afraid I don't have time to right now, but there's links to reference material in the comment above and here's an example of walking a DOM tree: http://stackoverflow.com/questions/5158022/wrap-a-tags-around-http-text/5158204#5158204 – T.J. Crowder Jun 19 '11 at 17:54
  • 1
    Can you give some more context to this question? It sounds to me like you are trying to do something that can be achieved in a better way. – Betamos Jun 19 '11 at 18:12
  • 1
    It'll be easy of you're using `jQuery` (or something similar). If you do, let us know – Makram Saleh Jun 19 '11 at 18:36

1 Answers1

1

XSLT will be better way to perform this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="*|@*|text()">
       <xsl:copy>
          <xsl:apply-templates select="*|@*|text()"/>
       </xsl:copy>
    </xsl:template>
    <xsl:template match="word">
        <a href="#" title="<xsl:copy-of select="data(@description)"/>">
          <xsl:copy-of select="data(.)"/></a>
    </xsl:template>
</xsl:stylesheet>

Contact me if any details required

Stanislav Levental
  • 2,165
  • 1
  • 14
  • 28