Unexpected behavior when creating xhtml documents

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marijn

    Unexpected behavior when creating xhtml documents

    Hey everybody,

    The following has been posted before (2 years ago) but no response was
    added. Therefor again, the following code for creating xhtml file:

    <?php

    error_reporting (6143);

    $xmlns = "http://www.w3.org/1999/xhtml";
    $lang = "en";

    $xhtml_1_strict = new DomImplementati on();

    $dtd_xhtml_1_st rict = $xhtml_1_strict->createDocument Type("html", "-//
    W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
    xhtml1-strict.dtd");

    $xhtml_1_strict _document = $xhtml_1_strict->createDocument ("", "",
    $dtd_xhtml_1_st rict);
    $xhtml_1_strict _document->encoding = "UTF-8";
    $xhtml_1_strict _document->standalone = "no";

    $html = $xhtml_1_strict _document->createElementN S($xmlns, "html");
    $html->setAttribute(" xml:lang", $lang);

    $head = $xhtml_1_strict _document->createElement( "head");
    $html->appendChild($h ead);

    $title = $xhtml_1_strict _document->createElement( "title", "Testing the
    DOM in PHP5");
    $head->appendChild($t itle);

    $base = $xhtml_1_strict _document->createElement( "base");
    $base->setAttribute(" href", "http://test.beatter.co m");
    $head->appendChild($b ase);

    $meta = $xhtml_1_strict _document->createElement( "meta");
    $meta->setAttribute(" http-equiv", "Content-Type");
    $meta->setAttribute(" content", "applicatio n/xhtml+xml;UTF-8");
    $head->appendChild($m eta);

    $body = $xhtml_1_strict _document->createElement( "body");
    $html->appendChild($b ody);

    $xhtml_1_strict _document->appendChild($h tml);

    header("Content-type: application/xhtml+xml;UTF-8");
    echo($xhtml_1_s trict_document->saveXML());

    ?>

    the expected output would be:

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>Testin g the DOM in PHP5</title><base href="http://
    test.beatter.co m" />
    <meta http-equiv="Content-Type" content="applic ation/xhtml
    +xml;UTF-8" />
    </head>
    <body>
    </body>
    </html>

    but the output is:

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=UTF-8" />
    <title>Testin g the DOM in PHP5</title><base href="http://
    test.beatter.co m" />
    </head>
    <body>
    </body>
    </html>

    Why does php add this node by itself? Why can't I override that
    behavior? Why does php change the order of outputing the elements (I'm
    appending, not pre-pending to the head element)?

    Thanks,

    Marijn

  • ZeldorBlat

    #2
    Re: Unexpected behavior when creating xhtml documents

    On Jun 6, 7:23 pm, Marijn <marijn.huizend v...@gmail.comw rote:
    Hey everybody,
    >
    The following has been posted before (2 years ago) but no response was
    added. Therefor again, the following code for creating xhtml file:
    >
    <?php
    >
    error_reporting (6143);
    >
    $xmlns = "http://www.w3.org/1999/xhtml";
    $lang = "en";
    >
    $xhtml_1_strict = new DomImplementati on();
    >
    $dtd_xhtml_1_st rict = $xhtml_1_strict->createDocument Type("html", "-//
    W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
    xhtml1-strict.dtd");
    >
    $xhtml_1_strict _document = $xhtml_1_strict->createDocument ("", "",
    $dtd_xhtml_1_st rict);
    $xhtml_1_strict _document->encoding = "UTF-8";
    $xhtml_1_strict _document->standalone = "no";
    >
    $html = $xhtml_1_strict _document->createElementN S($xmlns, "html");
    $html->setAttribute(" xml:lang", $lang);
    >
    $head = $xhtml_1_strict _document->createElement( "head");
    $html->appendChild($h ead);
    >
    $title = $xhtml_1_strict _document->createElement( "title", "Testing the
    DOM in PHP5");
    $head->appendChild($t itle);
    >
    $base = $xhtml_1_strict _document->createElement( "base");
    $base->setAttribute(" href", "http://test.beatter.co m");
    $head->appendChild($b ase);
    >
    $meta = $xhtml_1_strict _document->createElement( "meta");
    $meta->setAttribute(" http-equiv", "Content-Type");
    $meta->setAttribute(" content", "applicatio n/xhtml+xml;UTF-8");
    $head->appendChild($m eta);
    >
    $body = $xhtml_1_strict _document->createElement( "body");
    $html->appendChild($b ody);
    >
    $xhtml_1_strict _document->appendChild($h tml);
    >
    header("Content-type: application/xhtml+xml;UTF-8");
    echo($xhtml_1_s trict_document->saveXML());
    >
    ?>
    >
    the expected output would be:
    >
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>Testin g the DOM in PHP5</title><base href="http://
    test.beatter.co m" />
    <meta http-equiv="Content-Type" content="applic ation/xhtml
    +xml;UTF-8" />
    </head>
    <body>
    </body>
    </html>
    >
    but the output is:
    >
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=UTF-8" />
    <title>Testin g the DOM in PHP5</title><base href="http://
    test.beatter.co m" />
    </head>
    <body>
    </body>
    </html>
    >
    Why does php add this node by itself? Why can't I override that
    behavior? Why does php change the order of outputing the elements (I'm
    appending, not pre-pending to the head element)?
    >
    Thanks,
    >
    Marijn
    I've run into this problem, too. Unfortunately I've been unable to
    find /any/ documentation on how/where/why this happens. It also seems
    to depend on which version of the underlying XML libraries PHP is
    using. I, too, would love to hear from anyone that has some insight
    into this "feature."

    Comment

    • Marijn

      #3
      Re: Unexpected behavior when creating xhtml documents

      On Jun 7, 3:27 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
      On Jun 6, 7:23 pm, Marijn <marijn.huizend v...@gmail.comw rote:
      >
      >
      >
      Hey everybody,
      >
      The following has been posted before (2 years ago) but no response was
      added. Therefor again, the following code for creating xhtml file:
      >
      <?php
      >
      error_reporting (6143);
      >
      $xmlns = "http://www.w3.org/1999/xhtml";
      $lang = "en";
      >
      $xhtml_1_strict = new DomImplementati on();
      >
      $dtd_xhtml_1_st rict = $xhtml_1_strict->createDocument Type("html", "-//
      W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
      xhtml1-strict.dtd");
      >
      $xhtml_1_strict _document = $xhtml_1_strict->createDocument ("", "",
      $dtd_xhtml_1_st rict);
      $xhtml_1_strict _document->encoding = "UTF-8";
      $xhtml_1_strict _document->standalone = "no";
      >
      $html = $xhtml_1_strict _document->createElementN S($xmlns, "html");
      $html->setAttribute(" xml:lang", $lang);
      >
      $head = $xhtml_1_strict _document->createElement( "head");
      $html->appendChild($h ead);
      >
      $title = $xhtml_1_strict _document->createElement( "title", "Testing the
      DOM in PHP5");
      $head->appendChild($t itle);
      >
      $base = $xhtml_1_strict _document->createElement( "base");
      $base->setAttribute(" href", "http://test.beatter.co m");
      $head->appendChild($b ase);
      >
      $meta = $xhtml_1_strict _document->createElement( "meta");
      $meta->setAttribute(" http-equiv", "Content-Type");
      $meta->setAttribute(" content", "applicatio n/xhtml+xml;UTF-8");
      $head->appendChild($m eta);
      >
      $body = $xhtml_1_strict _document->createElement( "body");
      $html->appendChild($b ody);
      >
      $xhtml_1_strict _document->appendChild($h tml);
      >
      header("Content-type: application/xhtml+xml;UTF-8");
      echo($xhtml_1_s trict_document->saveXML());
      >
      ?>
      >
      the expected output would be:
      >
      <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
      <title>Testin g the DOM in PHP5</title><base href="http://
      test.beatter.co m" />
      <meta http-equiv="Content-Type" content="applic ation/xhtml
      +xml;UTF-8" />
      </head>
      <body>
      </body>
      </html>
      >
      but the output is:
      >
      <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
      <meta http-equiv="Content-Type" content="text/html;
      charset=UTF-8" />
      <title>Testin g the DOM in PHP5</title><base href="http://
      test.beatter.co m" />
      </head>
      <body>
      </body>
      </html>
      >
      Why does php add this node by itself? Why can't I override that
      behavior? Why does php change the order of outputing the elements (I'm
      appending, not pre-pending to the head element)?
      >
      Thanks,
      >
      Marijn
      >
      I've run into this problem, too. Unfortunately I've been unable to
      find /any/ documentation on how/where/why this happens. It also seems
      to depend on which version of the underlying XML libraries PHP is
      using. I, too, would love to hear from anyone that has some insight
      into this "feature."
      unfortunately DOM/XML documentation is very poor by itself. Especially
      concerning this topic. Is the problem solvable by installing another
      DOM/XML Library?

      Comment

      • Edward Z. Yang

        #4
        Re: Unexpected behavior when creating xhtml documents

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Marijn wrote:
        unfortunately DOM/XML documentation is very poor by itself. Especially
        concerning this topic. Is the problem solvable by installing another
        DOM/XML Library?
        Hi, you are welcome to submit specific concerns and clarifications to
        the PHP documentation team at news://news.php.net/php.doc

        The DOM extension is built off of libxml, so quirks like these are
        likely bugs in libxml and not DOM. Running your snippet on PHP 5.0.3,
        with libxml 2.6.11, I get (slightly formatted for readability):

        <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
        <!DOCTYPE html PUBLIC "-//
        W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/
        xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
        <title>Testin g the DOM in PHP5</title>
        <base href="http://test.beatter.co m"/>
        <meta http-equiv="Content-Type" content="applic ation/xhtml+xml;UTF-8"/>
        </head>
        <body/>
        </html>

        Which is the expected behavior. As you may already know, 5.2.3 and
        2.6.28 are the latest versions for PHP and libxml respectively, so you
        may be running on an ancient version of libxml. If this is the case,
        please upgrade or recompile the extension with a newer version of libxml.

        - --
        Edward Z. Yang GnuPG: 0x869C48DA
        HTML Purifier <htmlpurifier.o rg Anti-XSS HTML Filter
        [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.6 (MingW32)
        Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

        iD8DBQFGaK6XqTO +fYacSNoRAj8nAJ 94DFXP2vL53rYKZ E8oZpqr8JlpbACf WQgX
        5kpzr7VMP7744ol gYN8T9/M=
        =ESgZ
        -----END PGP SIGNATURE-----

        Comment

        • Marijn

          #5
          Re: Unexpected behavior when creating xhtml documents

          Hi, you are welcome to submit specific concerns and clarifications to
          the PHP documentation team at news://news.php.net/php.doc
          Might just do that. Will need to gain a little bit more self
          confidence about my code thouht ;)
          Which is the expected behavior. As you may already know, 5.2.3 and
          2.6.28 are the latest versions for PHP and libxml respectively, so you
          may be running on an ancient version of libxml. If this is the case,
          please upgrade or recompile the extension with a newer version of libxml.
          Looks like it is time I talked to my hosting provider <http://
          mediatemple.net >. Seem to be running 2.6.16 for libxml and php 5.2.2

          Thanks in advance

          Comment

          Working...