有人可以帮助我一些逻辑吗?

发布于 2024-12-14 00:07:45 字数 4268 浏览 3 评论 0原文

我无法发展这种逻辑,有人可以帮我解决这个问题吗?

我有一些大逻辑让我坚持下去。

我有一个完全动态的 XML - 即它的详细信息节点的数量可能会增加或减少。
并且细节节点的子节点数量也可以增减。

<?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>

我像这样读取 XML:

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

现在

每次我像这样从 DOM 读取 HTML 内容时,每次都会不同:

      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>

现在我想在此 HTML 中搜索 XML 中的每个详细元素,并希望替换 HTML 中的文本(名字、姓氏) ) 与 XML 中的元素值 每个细节节点的节点。
之后我会将格式化的 HTML 内容重写到 DOM 中。 这是我无法发展的逻辑,有人可以帮我解决这个问题吗?


我的努力:

<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>

和功能

    $(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);

    }

但它不适用于动态 XML,我必须提前修复节点...我想在运行时进行修复。

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  <strong> LastName</strong></strong></p>
<p><strong><br /></strong></p>
<p>           I am from company <strong> Company</strong> that is located in <strong> Country</strong>,  <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> </p>
<p>         </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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-12-21 00:07:45

我更愿意使用 JQuery 来实现此目的。它对于此类任务非常方便,您无需为每个浏览器编写代码。

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

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

感悟人生的甜 2024-12-21 00:07:45

您应该使用 xslt 生成内容而不是脚本,

如果您仍然想做您所做的事情, 我可以推荐以下内容:使用 {FirstName} 而不是 FirstName可以更好地找到它(出现 fo FirstName ,它不会被替换),顺便说一句,删除你的 xslt 并用 dom 循环 xml

,我也不认为你正在使用 new RegExp() 正确检查手册

b.tw 你说对于任何详细元素,但不可能,你必须选择 1 个详细元素并通过其同级循环,

这是很好的替换 js 中的代码(不知道 jquery (对不起))

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

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 b.t.w. drop your xslt and loop troug the xml with dom

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

b.t.w. 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文