За последние 24 часа нас посетили 23754 программиста и 1610 роботов. Сейчас ищут 940 программистов ...

Как восстановить завершающие слэши у одинарных тегов...

Тема в разделе "Прочие вопросы по PHP", создана пользователем Devzirom, 15 фев 2009.

  1. Devzirom

    Devzirom Активный пользователь

    С нами с:
    15 фев 2009
    Сообщения:
    463
    Симпатии:
    0
    Адрес:
    Пермь
    Как восстановить завершающие слэши у одинарных тегов в этом скрипте:
    PHP:
    1. <?php
    2. $imp = new DOMImplementation;
    3.  
    4. $dtd = $imp->createDocumentType("html",
    5.                 "-//W3C//DTD XHTML 1.0 Transitional//EN",
    6.                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
    7. $doc = $imp->createDocument("", "", $dtd);
    8.  
    9. $html = $doc->appendChild($doc->createElement("html"));
    10. $html_xmlns = $html->setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
    11.  
    12. $head = $html->appendChild($doc->createElement("head"));
    13.  
    14. $cache = $head->appendChild($doc->createElement("meta"));
    15. $cache_equiv = $cache->setAttribute("http-equiv", "Content-Type");
    16. $cache_content = $cache->setAttribute("content", "text/html; charset=utf-8");
    17.  
    18. $cache = $head->appendChild($doc->createElement("meta"));
    19. $cache_equiv = $cache->setAttribute("http-equiv", "Cache-Control");
    20. $cache_content = $cache->setAttribute("content", "no-cache");
    21.  
    22. $icon = $head->appendChild($doc->createElement("link"));
    23. $icon_href = $icon->setAttribute("shortcut-icon", "favicon.ico");
    24. $icon_type = $icon->setAttribute("type", "image/x-icon");
    25.  
    26. $title = $head->appendChild($doc->createElement("title"));
    27. $title_text = $title->appendChild($doc->createTextNode("This is the title"));
    28.  
    29. $body = $html->appendChild($doc->createElement("body"));
    30.  
    31. $header = $body->appendChild($doc->createElement("div"));
    32. $header_id = $header->setAttribute("id", "header");
    33. $header_h1 = $header->appendChild($doc->createElement("h1"));
    34. $header_h1_text = $header_h1->appendChild($doc->createTextNode("This is the header"));
    35.  
    36. $contenter = $body->appendChild($doc->createElement("div"));
    37. $contenter_id = $contenter->setAttribute("id", "contenter");
    38.  
    39. $contenter = $body->appendChild($doc->createElement("br"));
    40.  
    41. $footer = $body->appendChild($doc->createElement("div"));
    42. $footer_id = $footer->setAttribute("id", "header");
    43. $footer_anchor = $footer->appendChild($doc->createElement("a"));
    44. $footer_anchor_href = $footer_anchor->setAttribute("href", "http://www.example.com");
    45. $footer_anchor_title = $footer_anchor->setAttribute("title", "example");
    46. $footer_anchor_target = $footer_anchor->setAttribute("target", "_blank");
    47. $footer_anchor_text = $footer_anchor->appendChild($doc->createTextNode("Powered by example | example © 2008-2009, [url=http://www.example.com]www.example.com[/url]"));
    48.  
    49. $doc->normalizeDocument();
    50. print $doc->saveHTML();
    51. ?>
    Результат выполнения скрипта:
    HTML:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    4. <meta http-equiv="Cache-Control" content="no-cache">
    5. <link shortcut-icon="favicon.ico" type="image/x-icon">
    6. <title>This is the title</title>
    7. </head>
    8. <div id="header"><h1>This is the header</h1></div>
    9. <div id="contenter"></div>
    10. <br><div id="header"><a href="http://www.example.com" title="example" target="_blank">Powered by example | example © 2008-2009, [url=http://www.example.com]www.example.com[/url]</a></div>
    11. </body>
    12. </html>
    Как видите пропали завершающие слэши у одинарных тегов!

    Если использовать saveXML() вместо saveHTML(), получается белиберда - все теги в одну строку пишутся - очень не удобно просматривать получившийся код:

    HTML:
    1. <?xml version="1.0"?>
    2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    3. <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Cache-Control" content="no-cache" /><link shortcut-icon="favicon.ico" type="image/x-icon" /><title>This is the title</title></head><body><div id="header"><h1>This is the header</h1></div><div id="contenter"></div><br /><div id="header"><a href="http://www.example.com" title="example" target="_blank">Powered by example | example &#xA9; 2008-2009, [url=http://www.example.com]www.example.com[/url]</a></div></body></html>
    Есть выход из этой попы?
     
  2. Devzirom

    Devzirom Активный пользователь

    С нами с:
    15 фев 2009
    Сообщения:
    463
    Симпатии:
    0
    Адрес:
    Пермь
    Все разобрался, помог Доктор Штопор в этой теме
    http://php.ru/forum/viewtopic.php?p=143889
    Нужно поставить:
    formatOutput = true;
    PHP:
    1. $doc->formatOutput = true;
    Тогда при создании XML документа, на выходе будет отформатированный XHTML.
     
  3. Mr.M.I.T.

    Mr.M.I.T. Старожил

    С нами с:
    28 янв 2008
    Сообщения:
    4.586
    Симпатии:
    1
    Адрес:
    у тебя канфетка?
    это чё новый вид шаблонизатора 0о
    аля, зачем это надо?
     
  4. sergey144010

    sergey144010 Активный пользователь

    С нами с:
    25 май 2011
    Сообщения:
    1
    Симпатии:
    0
    Адрес:
    Россия
    Парни чувствую что у меня проблема где-то рядом с вашей, но пока немогу разобраться
    есть xsl файл, в нём строка вида
    Код (Text):
    1.  
    2. <xsl:value-of select='@vstavit' />
    есть php файл, часть кода которого
    Код (Text):
    1.  
    2. $m = $this->DOMDocument->getElementsByTagName("Document");
    3. $r = $m->item(0);
    4. $sp="<!-- -->";
    5. $r->setAttribute("vstavit",$sp);
    в получившемся html коде страницы вместо <---> получается
    &lt; --- &gt;
    каким образом реализовать вставку спецсимволов <> в исходный html код?
     
  5. [vs]

    [vs] Суперстар
    Команда форума Модератор

    С нами с:
    27 сен 2007
    Сообщения:
    10.559
    Симпатии:
    632
    DOM шаблонизатор это ппц, это вместо упрощения работы с представлением усложнение в 100500 раз