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!

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, DOM2 HTML, DOM3 Core Commented Jun 19, 2011 at 17:50
  • Can you show me how to do it, please! Commented Jun 19, 2011 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: stackoverflow.com/questions/5158022/… Commented Jun 19, 2011 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. Commented Jun 19, 2011 at 18:12
  • 1
    It'll be easy of you're using jQuery (or something similar). If you do, let us know Commented Jun 19, 2011 at 18:36

1 Answer 1

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

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

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.