Verschachtelte, hierarchische UL / LI-Listen auf eine Ebene reduzieren. Flatten hierarchical LI-lists to linear list.

Für ein web2print-Projekt, das verschachtelte HTML-Listen in Tags für InDesign umwandeln sollte, sind wir auf ein Problem gestossen: InDesign stellt verschachtelte Listen nicht wirklich durch verschachtelte Tag-Elemente dar, sondern man erstellt vorab für jede Hierarchie in der Bullet-Liste ein eigenes Absatzformat. Die Herausforderung lag also darin, aus dem HTML-Input per PHP ein XML-Output zu generieren, in dem die Tiefe der Listen-Verschachtelungen zu “flachen” (flattened) Absatzformaten mit Kennzeichnung der Tiefe umgewandelt werden.

Ein Beispiel macht die Idee deutlich:
Aus einem HTML-Dokument mit einer klassischen, verschachtelten Liste…

Das ist eine Liste:
<ul>
   <li>Kapitel 1</li>
   <li>Kapitel 2
      <ul>
         <li>Kapitel 2.1</li>
         <li>Kapitel 2.2
            <ul>
               <li>Kapitel 2.2.1</li>
            </ul>
         </li>
      </ul>
   </li>
   <li>Kapitel 3</li>
</ul>
Viel Spass damit!

…sollte etwas in dieser Art generiert werden:

Das ist eine Liste:
<p class="depth-0">Kapitel 1</p>
<p class="depth-0">Kapitel 2</p>
<p class="depth-1">Kapitel 2.1</p>
<p class="depth-1">Kapitel 2.2</p>
<p class="depth-2">Kapitel 2.2.1</p>
<p class="depth-0">Kapitel 3</p>
Viel Spass damit!

Die Lösung setzt phpQuery.php ein – eine (geniale) Adaption der (noch genialeren) jQuery-Bibliothek für PHP statt JavaScript. Die Klasse mit entsprechender Rekursion dazu sah in PHP so aus:

<?php
require_once('phpQuery.php');
class renderEngine {
 
   function flatten_nested_lists ( $txt ) {
      $nodes = phpQuery::newDocument( $txt );
      $this->flatten_nested_lists_recursion( $nodes );
      return $nodes->html();
   }
 
   function flatten_nested_lists_recursion ( $el, $depth = 0) {
      $arr = [];
      foreach ($el->children('ul') as $cnt_ul=>$ul) {
       $ul = pq($ul);
        $arr = [];
        foreach ($ul->children('li') as $cnt_li=>$li) {
        $li = pq($li);
        $li->attr('depth', $depth);
        $liClone = $li->clone();
        $liClone->find('ul')->remove();
        $str = trim($liClone->attr('depth').' - '.$liClone->html());
        $arr[] = '<p aid:pstyle="Bullet_Lv_'.$liClone->attr('depth').'">'.$str.'</p>';
        $arr = array_merge($arr, $this->flatten_nested_lists_recursion($li, $depth+1));
      }
      foreach ($arr as $n) {
        $ul->before($n);
      }
      $ul->remove();
    }
    $el->children('ul')->remove();
    return $arr;
   }
}

…und in Funktion dann so:

 
$Engine = new renderEngine();
$input = '...hier der HTML-Code mit ul/li rein...';
echo $Engine->flatten_nested_lists($input);

36 Antworten zu “Verschachtelte, hierarchische UL / LI-Listen auf eine Ebene reduzieren. Flatten hierarchical LI-lists to linear list.”

  1. gamma 11 blue…

    Verschachtelte, hierarchische UL / LI-Listen auf eine Ebene reduzieren. Flatten hierarchical LI-lists to linear list. « 99° Labor…

  2. http://mail-genie.com/asics-33-review-into-the-woods.html

    You really make it appear really easy along with your presentation but I in finding this topic to be really something which I feel I might never understand. It kind of feels too complex and very wide for me. I’m having a look ahead on your next put up…

  3. http://columbia.se/asics-kayano-cheap-4-wheeler.html

    You really make it seem so easy along with your presentation however I in finding this matter to be really one thing that I feel I might by no means understand. It seems too complicated and very vast for me. I’m looking ahead to your subsequent submit…

  4. http://www.telnetdealers.com/asics-fuji-packable-jacket-review.html

    You actually make it seem so easy with your presentation but I in finding this matter to be really one thing which I feel I would never understand. It kind of feels too complicated and extremely huge for me. I am looking ahead on your next put up, I wi…

  5. http://www.eugeniosideri.com/asics-nimbus-14-womens-online.html

    You really make it appear really easy along with your presentation but I in finding this matter to be actually one thing that I think I’d by no means understand. It kind of feels too complicated and extremely huge for me. I’m looking forward for your…

  6. http://admin.eturizam.net/asics-33-dfa-review-comment.html

    You actually make it seem really easy together with your presentation however I to find this matter to be really something which I believe I would by no means understand. It sort of feels too complex and very extensive for me. I am having a look forwar…

  7. http://www.pipetechitalia.com/asics-nimbus-limited-wireless-connectivity.html

    You actually make it seem really easy with your presentation but I find this topic to be actually one thing which I think I might never understand. It kind of feels too complex and very wide for me. I’m looking ahead on your next put up, Iˇll try to g…

  8. asics matflex wrestling shoes…

    You actually make it seem really easy with your presentation however I find this topic to be actually something that I believe I’d never understand. It seems too complex and extremely large for me. I’m taking a look ahead in your subsequent submit, I…

  9. asics foundation healthcare…

    You actually make it appear so easy with your presentation however I to find this matter to be actually something which I feel I might by no means understand. It sort of feels too complex and extremely wide for me. I am having a look forward in your su…

  10. asics official site 212…

    You really make it seem really easy along with your presentation but I find this matter to be really something which I feel I would never understand. It kind of feels too complex and extremely extensive for me. I am having a look forward on your next p…

  11. asics nimbus history jay-z…

    You actually make it seem so easy together with your presentation however I in finding this topic to be actually something that I believe I would never understand. It seems too complex and extremely large for me. I am taking a look ahead in your next p…

  12. linda hogan y su nuevo novio…

    You actually make it seem really easy with your presentation however I to find this matter to be actually one thing that I think I’d never understand. It seems too complicated and extremely vast for me. I am looking forward to your subsequent post, I …

  13. hogan da uomo…

    You actually make it appear really easy along with your presentation however I in finding this topic to be actually one thing which I feel I’d by no means understand. It sort of feels too complicated and very large for me. I’m looking forward to your…

  14. asics volleyball shoes 06790…

    You actually make it seem really easy along with your presentation however I in finding this topic to be actually one thing which I feel I might by no means understand. It sort of feels too complex and very large for me. I’m looking forward in your su…

  15. asics badminton shoes kids…

    You actually make it appear so easy with your presentation but I find this topic to be actually one thing that I feel I’d never understand. It sort of feels too complex and extremely vast for me. I am looking ahead for your subsequent put up, Iˇll try…

  16. hogan interactive grey argilla…

    You actually make it appear really easy along with your presentation however I in finding this topic to be really something which I believe I might by no means understand. It seems too complex and extremely vast for me. I’m taking a look ahead to your…

  17. asics nimbus 25 hour…

    You really make it appear really easy together with your presentation but I in finding this topic to be actually something that I feel I’d never understand. It seems too complex and extremely large for me. I am looking ahead on your subsequent publish…

  18. come riconoscere le scarpe hogan originali…

    You really make it seem really easy with your presentation however I find this topic to be really one thing that I believe I’d by no means understand. It sort of feels too complicated and extremely broad for me. I am taking a look forward to your next…

  19. asics shoes 10 5 on demand…

    You really make it seem really easy along with your presentation but I find this matter to be actually one thing which I feel I might never understand. It sort of feels too complicated and very vast for me. I am taking a look ahead for your next submit…

  20. paul hogan london metropolitan…

    You really make it seem so easy with your presentation but I to find this matter to be actually something which I think I might by no means understand. It seems too complicated and extremely vast for me. I am looking ahead in your next publish, Iˇll at…

  21. katie hogan facebook…

    You really make it seem really easy with your presentation however I in finding this matter to be actually one thing which I believe I might never understand. It sort of feels too complex and extremely vast for me. I am taking a look forward in your ne…

  22. asics shoes gel que…

    You really make it appear so easy with your presentation but I to find this topic to be actually one thing that I think I’d never understand. It sort of feels too complicated and very broad for me. I’m having a look ahead for your subsequent submit, …

  23. hogan a roma via borgognona…

    You really make it appear so easy with your presentation however I to find this topic to be really one thing which I think I would by no means understand. It kind of feels too complex and very wide for me. I am having a look forward in your next publis…

  24. outlet hogan vicenza…

    You really make it seem so easy together with your presentation but I in finding this topic to be really one thing which I feel I’d by no means understand. It seems too complicated and very huge for me. I am having a look forward for your next post, I…

  25. scarpe hogan roma centro…

    Thank you for every other great post. The place else may just anybody get that type of info in such a perfect approach of writing? I’ve a presentation next week, and I am at the look for such info. Itˇs actually a nice and useful piece of info. Iˇm ha…

  26. hogan wings boeing 777…

    hi!,I like your writing very much! share we communicate extra approximately your article on AOL? I need a specialist on this house to resolve my problem. Maybe that’s you! Taking a look ahead to peer you. http://www.nucleovega.com/pg.php?hg7u4g http:/…

  27. quanto costano le hogan nuove…

    You really make it seem really easy with your presentation however I to find this topic to be really one thing that I feel I’d never understand. It sort of feels too complex and very large for me. I am having a look forward to your next post, I will t…

  28. hogan brand wikipedia…

    Iˇve been exploring for a little bit for any high quality articles or blog posts in this kind of space . Exploring in Yahoo I eventually stumbled upon this site. Studying this information So iˇm satisfied to convey that I have an incredibly excellent u…

  29. zapatos hogan mujer 2013…

    You really make it seem so easy with your presentation however I find this matter to be really something which I feel I’d by no means understand. It kind of feels too complex and very vast for me. I’m taking a look ahead to your subsequent post, Iˇll…

  30. hogan rebel uomo estate 2014…

    You actually make it appear so easy along with your presentation but I in finding this matter to be actually one thing which I feel I might by no means understand. It kind of feels too complex and extremely wide for me. I’m looking forward for your su…

  31. hogan bambino on line…

    Thank you a bunch for sharing this with all folks you actually recognize what you’re talking about! Bookmarked. Please also seek advice from my web site =). We may have a hyperlink alternate agreement among us! http://www.hnidoor.com/pg.php?hgsjh6 htt…

  32. TommyRes sagt:

    Lose up to 23 pounds of pure body fat in just 3 weeks!, how did i do that?
    The 3 Week Diet is an extreme rapid weight loss program that can help you lose up to 23 pounds of pure body fat in just 3 weeks! Get your customized diet plan from our official website and find out which foods you should be eating if you want to burn your belly fat away fast! Created by acclaimed personal trainer and nutritionist Brian Flatt, The 3 Week Diet is 100% guaranteed to deliver results!
    look great? dont waste your time! lets do this, join with us and bet back your beautiful body at here:

    http://polr.me/1f0e
    Thx

  33. BrosEn sagt:

    Tim McGraw is a famous country singer, so don’t miss the possibility to visit Tim McGraw tickets nc

  34. JennEn sagt:

    Zac Brown Band is a famous country singer, so don’t miss the possibility to visit Zac Brown Band concert video

  35. BrosEn sagt:

    Tim McGraw is a famous country singer, so don’t miss the possibility to visit Tim McGraw concert wichita kansas

  36. AnEn sagt:

    Jason Aldean is a famous country singer, so don’t miss the possibility to visit Jason Aldean tour wichita ks

Eine Antwort hinterlassen