PHP saving embedded HTML in XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dcrackel@gmail.com

    PHP saving embedded HTML in XML

    I hope there is a simple solution to this, but I've been unable to
    find it.


    $dom = new DomDocument();
    $dom->load("test.xml ");

    $test = $dom->getElementsByT agName("test");
    $test->nodeValue = "<b>test</b>";
    $dom->save("test.xml ");


    I would like the node in the xml file to look like:
    <test><b>test </b></test>

    Rather than the encoded version.
    &lt;b&gt;test&l t;/b&gt;

    What can be done to accomplish this task?

    Thank you
    dwain

  • Ian Taylor

    #2
    Re: PHP saving embedded HTML in XML

    dcrackel@gmail. com wrote:
    I hope there is a simple solution to this, but I've been unable to
    find it.
    >
    >
    $dom = new DomDocument();
    $dom->load("test.xml ");
    >
    $test = $dom->getElementsByT agName("test");
    $test->nodeValue = "<b>test</b>";
    $dom->save("test.xml ");
    >
    >
    I would like the node in the xml file to look like:
    <test><b>test </b></test>
    >
    Rather than the encoded version.
    &lt;b&gt;test&l t;/b&gt;
    >
    What can be done to accomplish this task?
    >
    Thank you
    dwain
    >
    From what I can see, it's not really possible - if it was possible, and
    you tried to re-read the resulting xml file back, you would end up with
    something like:

    ....
    <test>
    <b>
    test
    </b>
    </test>
    ....

    where <bis a child node of the <testnode, rather than part of the
    value of the <testnode.

    Therefore, entities like < and do need to be encoded / decoded
    (possible with html_entity_dec ode()) when dealing with xml.

    If, however, you *are* looking to add <bas a child node of <test>, I'm
    sure there are functions to handle that ( probably something like $child
    = $test->append_child(' b') )

    Comment

    • dcrackel@gmail.com

      #3
      Re: PHP saving embedded HTML in XML

      On Feb 21, 12:37 pm, Ian Taylor <m...@ocset.rev erse.previous.w ord.net>
      wrote:
      dcrac...@gmail. com wrote:
      I hope there is a simple solution to this, but I've been unable to
      find it.
      >
      $dom = new DomDocument();
      $dom->load("test.xml ");
      >
      $test = $dom->getElementsByT agName("test");
      $test->nodeValue = "<b>test</b>";
      $dom->save("test.xml ");
      >
      I would like the node in the xml file to look like:
      <test><b>test </b></test>
      >
      Rather than the encoded version.
      &lt;b&gt;test&l t;/b&gt;
      >
      What can be done to accomplish this task?
      >
      Thank you
      dwain
      >
      From what I can see, it's not really possible - if it was possible, and
      you tried to re-read the resulting xml file back, you would end up with
      something like:
      >
      ...
      <test>
      <b>
      test
      </b>
      </test>
      ...
      >
      where <bis a child node of the <testnode, rather than part of the
      value of the <testnode.
      >
      Therefore, entities like < and do need to be encoded / decoded
      (possible with html_entity_dec ode()) when dealing with xml.
      >
      If, however, you *are* looking to add <bas a child node of <test>, I'm
      sure there are functions to handle that ( probably something like $child
      = $test->append_child(' b') )- Hide quoted text -
      >
      - Show quoted text -

      Thank you for the advice,

      I'm looking to save HTML inside the XML, for that example that text
      may be viewed bold when shown in a browser.

      ------------------------------------ Test.xml
      <?xml version="1.0"?>
      <ROOT>
      <TEST>
      <NAME>TEST PRODUCT 01</NAME>
      <DESC>This is a test product, this is <b>bold</b>.</DESC>
      <PRICE>9.99</PRICE>
      </TEST>
      </ROOT>

      ------------------------------------ Test.xsl
      <?xml version="1.0"?>
      <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/
      Transform">
      <xsl:output method="html"/>

      <xsl:template match="/">
      <html>
      <xsl:for-each select="ROOT/TEST">
      <P>
      <hr />
      <xsl:value-of select="NAME" /<br />
      <xsl:copy-of select="DESC |text()"/ <br />
      <xsl:value-of select="PRICE" /<br />
      <hr />

      </P>
      </xsl:for-each>

      </html>
      </xsl:template>
      </xsl:stylesheet>

      ------------------------------------------------------- Test.php
      <?php

      $xsl = new DomDocument();
      $xsl->load("test.xsl ");

      $inputdom = new DomDocument();
      $inputdom->load("test.xml ");

      $proc = new XsltProcessor() ;
      $xsl = $proc->importStyleshe et($xsl);

      $newdom = $proc->transformToDoc ($inputdom);
      print $newdom->saveXML();
      ?>

      ----------------------------- Result
      <?xml version="1.0" standalone="yes "?>
      <html><P><hr/>TEST PRODUCT 01<br/>

      <DESC>This is a test product, this is <b>bold</b>.</DESC>

      <br/>9.99<br/><hr/></P></html>



      -------------------------------- The problem

      When a XML document is saved by PHP it looks like

      <?xml version="1.0"?>
      <ROOT>
      <TEST>
      <NAME>TEST PRODUCT 01</NAME>
      <DESC>This is a test product, this is &lt;b&gt;bold&l t;/b&gt;.</
      DESC>
      <PRICE>9.99</PRICE>
      </TEST>
      </ROOT>



      I need to be able to save the XML Doc after altering it in PHP:
      EX:
      $node->nodeValue = "This is a test product, this is <b>bold</b>.";
      $dom->save("test.xml ");

      without encoding the "<b>"'s.



      Comment

      • dcrackel@gmail.com

        #4
        Re: PHP saving embedded HTML in XML

        I have 2 different fixes, one is to a PHP function to swap the chaters
        as needed:

        function fixXML($string) {

        for($i=1; $i < strlen($string) ;$i++) {
        $subs1 = substr ($string, $i, 4);

        switch ($subs1) {
        case "&gt;":
        $string = substr ($string, 0, $i) . ">" . substr ($string, $i +4,
        strlen($string) );
        break;
        case "&lt;":
        $string = substr ($string, 0, $i) . "<" . substr ($string, $i +4,
        strlen($string) );
        break;
        }
        }// end for
        return $string;
        }

        <... code ..>

        print fixXML($dom->saveXML());


        and another way it can be done in the XSL. I thought this method was
        allot messer.... and you would need to call for each charater you need
        to swap out.

        Snips from test.xsl
        --------

        <xsl:variable name="myString" select="HEADLIN E" />
        <xsl:variable name="myNewStri ng">
        <xsl:call-template name="Substring Replace">
        <xsl:with-param name="stringIn" select="$myStri ng" />
        <xsl:with-param name="substring In" select="'&lt;'"/>
        <xsl:with-param name="substring Out" select="'%3C'"/>

        </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="concat( ' input: ',$myString,' output: ',
        $myNewString)" />


        <xsl:template name="Substring Replace">
        <xsl:param name="stringIn" />
        <xsl:param name="substring In" />
        <xsl:param name="substring Out" />
        <xsl:choose>
        <xsl:when test="contains( $stringIn,$subs tringIn)">
        <xsl:value-of select="concat( substring-before($stringI n,
        $substringIn),$ substringOut)" />
        <xsl:call-template name="Substring Replace">
        <xsl:with-param name="stringIn" select="substri ng-after($stringIn ,
        $substringIn)" />
        <xsl:with-param name="substring In" select="$substr ingIn" />
        <xsl:with-param name="substring Out" select="$substr ingOut" />
        </xsl:call-template>
        </xsl:when>
        <xsl:otherwis e>
        <xsl:value-of select="$string In" />
        </xsl:otherwise>
        </xsl:choose>
        </xsl:template>



        I hope this helps someone.

        good luck

        Comment

        • Tim Hunt

          #5
          Re: PHP saving embedded HTML in XML

          On Feb 21, 5:37 pm, Ian Taylor <m...@ocset.rev erse.previous.w ord.net>
          wrote:
          dcrac...@gmail. com wrote:
          I hope there is a simple solution to this, but I've been unable to
          find it.
          >
          $dom = new DomDocument();
          $dom->load("test.xml ");
          >
          $test = $dom->getElementsByT agName("test");
          $test->nodeValue = "<b>test</b>";
          $dom->save("test.xml ");
          >
          I would like the node in the xml file to look like:
          <test><b>test </b></test>
          >
          Rather than the encoded version.
          &lt;b&gt;test&l t;/b&gt;
          >
          What can be done to accomplish this task?
          >
          Thank you
          dwain
          >
          From what I can see, it's not really possible - if it was possible, and
          you tried to re-read the resulting xml file back, you would end up with
          something like:
          >
          ...
          <test>
          <b>
          test
          </b>
          </test>
          ...
          >
          where <bis a child node of the <testnode, rather than part of the
          value of the <testnode.
          >
          Therefore, entities like < and do need to be encoded / decoded
          (possible with html_entity_dec ode()) when dealing with xml.
          >
          If, however, you *are* looking to add <bas a child node of <test>, I'm
          sure there are functions to handle that ( probably something like $child
          = $test->append_child(' b') )
          Hi

          You can do it like Ian said:

          $elem = $dom->createElement( 'b');
          $elem->nodeValue = 'test';
          $test->appendChild($e lem);

          Or using a document fragment

          $fragment = $dom->createDocument Fragment();
          $fragment->appendXml('<b> test</b>');
          $test->appendChild($f ragment);

          Comment

          Working...