Ho un codice di base che può eseguire il looping di alcuni XML generati da Adobe RoboHelp (per la nostra documentazione di aiuto). Funziona bene, ma dal momento che un argomento può essere annidato quante volte vuole lo scrittore, ho bisogno di un modo migliore per scorrere questo XML piuttosto che semplicemente annidare i loop .each()
.Looping tramite XML con jQuery
Ecco cosa l'XML assomiglia
<?xml version="1.0" encoding="utf-8"?>
<!--RoboML: Table of Content-->
<roboml_toc>
<page title="Welcome" url="Welcome.htm"/>
<book title="Getting Started" url="Getting_Started/Initial_Setup.htm">
<page title="Initial Setup" url="Getting_Started/Initial_Setup.htm"/>
<page title="Customize Settings" url="Getting_Started/Settings.htm"/>
</book>
<book title="Administrator Services" url="Administrator_Services/General_Administrator.htm">
<book title="Portal Workspace" url="Administrator_Services/Portal_Workspace/AdminHome.htm">
<page title="Home" url="Administrator_Services/Portal_Workspace/AdminHome.htm"/>
<page title="Portal Accounts" url="Administrator_Services/Portal_Workspace/Portal_Accounts.htm"/>
</book>
<book title="SpamLab" url="Administrator_Services/SpamLab/SpamLab_Admin_General.htm">
<page title="Alerts" url="Administrator_Services/SpamLab/Alerts.htm"/>
<page title="Spam Quarantine" url="Administrator_Services/SpamLab/Admin_Spam_Quarantine_.htm"/>
</book>
</book>
<book title="User Services" url="User_Services/General_User.htm">
<book title="Portal Workspace" url="User_Services/Portal_Workspace/Home.htm">
<page title="Home" url="User_Services/Portal_Workspace/Home.htm"/>
<page title="Self Help" url="User_Services/Portal_Workspace/Self_Help.htm"/>
</book>
<book title="SpamLab" url="User_Services/SpamLab/SpamLab_General.htm">
<page title="Spam Quarantine" url="User_Services/SpamLab/Spam_Quarantine.htm"/>
<page title="Virus Quarantine" url="User_Services/SpamLab/Virus_Quarantine.htm"/>
</book>
<book title="Encryption" url="User_Services/Encryption/Encryption_General.htm">
<page title="Outlook Plug-in" url="User_Services/Encryption/Encryption_Outlook_Plug_in.htm"/>
</book>
</book>
</roboml_toc>
Un <page>
è un articolo, ed un <book>
è una cartella.
Il suo è il mio codice jQuery, che solo può guardare un livello profondo di tag
//Get the TOC
$tocOutput="";
$.get(tocURL,function(toc){
$(toc).children().each(function(){
$tocOutput+="<li><a href='"+$(this).attr("url")+"'>"+$(this).attr("title")+"</a>";
if(this.tagName=="BOOK"){
$tocOutput+="<ul>";
$(this).find("page").each(function(){
$tocOutput+="<li><a href='"+$(this).attr("url")+"'>"+$(this).attr("title")+"</a></li>";
});
$tocOutput+="</ul>";
}
$tocOutput+="</li>";
});
$("#list").html($tocOutput);
So che c'è un modo migliore per appena scorrere tutti gli elementi e quindi determinare se l'elemento ha dei figli, ecc ma io non riesco a pensare a come farlo.
Qualsiasi aiuto è molto apprezzato!
Solo curioso: perché questo deve essere fatto sul client? Perché non applicare una trasformazione XSLT sul server e inviare l'html? La dinamica xml? L'html trasformato non può essere memorizzato nella cache sul server? –
In ogni caso, felice che tu abbia trovato una soluzione - ORA ACCETTIAMO LA RISPOSTA DI KEITH :) –
RIFIUTO! Questa era solo una prova di concetto per ora ... in realtà volevo solo impressionare alcune persone. Adesso mi stanno lusingando di lodi, quindi ne vale la pena. Alla fine penso che faremo comunque questo lato server. –