简体   繁体   中英

Can somebody help me with some logic?

I am unable to develop this logic, could somebody help me out with this?

I have some big logic to make that I am stuck on.

I have an XML that is totally dynamic - that is its Detail node may increase and decrease in number.
And detail nodes' sub-nodes can also increase and decrease in number.

<?xml version="1.0" encoding="utf-8" ?>
<body>
  <detail>
    <FirstName>t1 </FirstName>
    <LastName>t2</LastName>
    <Company>t3</Company>
    <Country>t4</Country>
    <Proviance>MP</Proviance>
  </detail>

  <detail>
    <FirstName>t5 </FirstName>
    <LastName>t6</LastName>
    <Company>t7</Company>
    <Country>t8</Country>
    <Proviance>t9</Proviance>
  </detail>

  <detail>
    <FirstName>t10 </FirstName>
    <LastName>t11</LastName>
    <Company>t12</Company>
    <Country>t13</Country>
    <Proviance>t14</Proviance>
  </detail>

</body>

I read XML like this:

 xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "/TinyEditor/PreviewBody.xml", true);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

Now

Every time I read HTML content from the DOM like this, it is different every time:

      var x = tinyMCEPopup.editor.getContent().toString();
        alert(x);

the x gets the value like this (x can be different at different times) 

<p>Headline Dear <br /> <br /> <img src="Images/Untitled.png" alt="" /> <br /> <br />Dear <strong>FirstName&nbsp; <strong> LastName</strong></strong></p>
<p><strong><br /></strong></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I am from company <strong> Company</strong> that is located in <strong> Country</strong>,&nbsp; <strong> Proviance</strong>, <strong> City.</strong></p>
<p><strong><br /></strong></p>
<p>Thanks</p>
<p><strong>FirstName</strong> <strong> LastName</strong> </p>
<p><strong><br /></strong></p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>

Now I want to search for every detail element in XML in this HTML and want to replace text in HTML (FirstName, LastName) with the element value in XML nodes for each detail Node.
After that I will rewrite the formatted HTML content to the DOM. This is the logic I am unable to develop, could somebody help me out with this?


My efforts:

<xsl:for-each select="Home/menu">
    <xsl:variable name="i"><xsl:value-of select="1+position()" /></xsl:variable>
    <script language="javascript">
        var id = '<xsl:value-of select="$i"/>';
        var firstname = '<xsl:value-of select="firstname"/>';
        var lastrname = '<xsl:value-of select="lastrname"/>';
        var firstname = '<xsl:value-of select="firstname"/>';
        var lastrname = '<xsl:value-of select="lastrname"/>';
        var firstname = '<xsl:value-of select="firstname"/>';
        var lastrname = '<xsl:value-of select="lastrname"/>';
        $('#CurrProg-'+id).html(functionname('FirstName',firstname));
        $('#CurrProg-'+id).html(functionname('LastName',firstname));
        $('#CurrProg-'+id).html(functionname('tagname',firstname));
        $('#CurrProg-'+id).html(functionname('tagname',firstname));
        $('#CurrProg-'+id).html(functionname('tagname',firstname));
        $('#CurrProg-'+id).html(functionname('tagname',firstname));
    </script>
</xsl:for-each>

and function

    $(document).ready(function () {

        /* load all xsl and xml file */

        xmlDoc = $.xsl.load('XML/PreviewBody.xml');
        xslHome = $.xsl.load('XSL/Preview.xsl');

        $('#Page_Content').getTransform(
    xslHome,
    xmlDoc
);

    });

 function textReplace(actualtext, replacementtext) {
        var actualtext = new RegExp(actualtext, 'ig');
        document.getElementById('content').innerHTML = x.replace(actualtext, replacementtext);

    }

But it doesn't work for dynamic XML, I have to fix node in advance... I want to make it at run time.

I would prefer to use JQuery for that purpose. Its quite handy for such tasks and you don't need to craft your code for every browser.

http://www.vagrantradio.com/2009/10/how-to-parse-xml-using-jquery-and-ajax.html

you should use the xslt to generate you content not your script

if you still want to do whart you do i can recocommend the forllowing: use {FirstName} instead of FirstName as you can find it better(an occurrence fo FirstName which sould not be replaced) and btw drop your xslt and loop troug the xml with dom

also i don't think you are using new RegExp() right check manuals

btw you say for any detail elelemnt but is not posseble you must select 1 detail-element and loop trough its siblings

this is good replace code in js (don't know about jquery (sorry))

function textReplace(actualtext, replacementtext) {
  document.getElementById('content').innerHTML = document.getElementById('content').innerHTML.replace(actualtext,replacementtext);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM