[PHP5 & DOM] element extracted from doc suddenly not found in insertBefore() -- NS problem

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

    [PHP5 & DOM] element extracted from doc suddenly not found in insertBefore() -- NS problem

    Code and problem are from PHP5:

    When I execute the following piece of code, the DomException is thrown with
    a message: Not Found Exception.
    I assume this means that the node I extracted from the DomDocument using
    getElementsByTa gName() isn't found when I use insertBefore(). I blame the
    namespace :-)
    When I replace the getElementsByTa gName( 'xsl:template' ), I get the "no
    template tag found message" in the else.
    What is the NS-safe way to insert a xsl:param tag before the xsl:template
    tags?

    <?php

    $xsl = <<<XSL
    <?xml version="1.0" ?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:import href="xhtml/basic.xhtml.xsl " />
    <xsl:import href="xhtml/search.xhtml.xs l" />
    <xsl:import href="xhtml/news.xhtml.xsl" />

    <xsl:output
    method="xml"
    omit-xml-declaration="no "
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    indent="yes"
    media-type="text/html"
    />

    <xsl:param name="updatetim e" />

    <xsl:template match="/">
    <xsl:apply-imports />
    </xsl:template>

    </xsl:stylesheet>
    XSL;

    $stylesheet = DomDocument::lo adXML( $xsl );
    $temp_list = $stylesheet->getElementsByT agName( 'template' );
    if( $temp_list && $temp_list->item(0) ) {
    $template = $temp_list->item(0);
    $param = $stylesheet->createElemen t( 'xsl:param' );
    try {
    $stylesheet->insertBefore ( $param, $template );
    } catch( DomException $e ) {
    print "Exception ".$e->getMessage() ;
    print "\n";
    print $template->tagName;
    }

    }
    else {
    die( "No template tag found" );
    }
    ?>

    Thanks in advance.
    Rutger Claes
    --
    Rutger Claes rgc@rgc.tld
    Replace tld with top level domain of belgium to contact me pgp:0x3B7D6BD6
    Overflow on /dev/null, please empty the bit bucket.

  • konsu

    #2
    Re: [PHP5 &amp; DOM] element extracted from doc suddenly not found in insertBefore() -- NS problem

    there must be a method similar to createElement() which creates an element
    in a given namespace. also check out


    konstantin

    "Rutger Claes" <rgc@rgc.tld-of-belgium> wrote in message
    news:1101891341 .571656@seven.k ulnet.kuleuven. ac.be...[color=blue]
    > Code and problem are from PHP5:
    >
    > When I execute the following piece of code, the DomException is thrown
    > with
    > a message: Not Found Exception.
    > I assume this means that the node I extracted from the DomDocument using
    > getElementsByTa gName() isn't found when I use insertBefore(). I blame the
    > namespace :-)
    > When I replace the getElementsByTa gName( 'xsl:template' ), I get the "no
    > template tag found message" in the else.
    > What is the NS-safe way to insert a xsl:param tag before the xsl:template
    > tags?
    >
    > <?php
    >
    > $xsl = <<<XSL
    > <?xml version="1.0" ?>
    > <xsl:styleshe et version="1.0"
    > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:import href="xhtml/basic.xhtml.xsl " />
    > <xsl:import href="xhtml/search.xhtml.xs l" />
    > <xsl:import href="xhtml/news.xhtml.xsl" />
    >
    > <xsl:output
    > method="xml"
    > omit-xml-declaration="no "
    > doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    > indent="yes"
    > media-type="text/html"
    > />
    >
    > <xsl:param name="updatetim e" />
    >
    > <xsl:template match="/">
    > <xsl:apply-imports />
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > XSL;
    >
    > $stylesheet = DomDocument::lo adXML( $xsl );
    > $temp_list = $stylesheet->getElementsByT agName( 'template' );
    > if( $temp_list && $temp_list->item(0) ) {
    > $template = $temp_list->item(0);
    > $param = $stylesheet->createElemen t( 'xsl:param' );
    > try {
    > $stylesheet->insertBefore ( $param, $template );
    > } catch( DomException $e ) {
    > print "Exception ".$e->getMessage() ;
    > print "\n";
    > print $template->tagName;
    > }
    >
    > }
    > else {
    > die( "No template tag found" );
    > }
    > ?>
    >
    > Thanks in advance.
    > Rutger Claes
    > --
    > Rutger Claes rgc@rgc.tld
    > Replace tld with top level domain of belgium to contact me
    > pgp:0x3B7D6BD6
    > Overflow on /dev/null, please empty the bit bucket.
    >[/color]


    Comment

    • Rutger Claes

      #3
      Re: [PHP5 &amp; DOM] element extracted from doc suddenly not found in insertBefore() -- NS problem

      konsu wrote:
      [color=blue]
      > there must be a method similar to createElement() which creates an element
      > in a given namespace. also check out
      >[/color]
      http://us2.php.net/manual/en/functio...ytagnamens.php[color=blue]
      >
      > konstantin
      >[/color]

      If you replace the code with all the ...NS() functions the result is
      identical. The problem is that when you extract a node from the document
      and use it to insert another, the node suddenly is not found.

      New code:
      $xml = DomDocument::lo adXML( $xsl );

      $t_list =
      $xml->getElementsByT agnameNS( 'http://www.w3.org/1999/XSL/Transform',
      'template' );
      if( $t_list && $t_list->item(0) ) {
      print $t_list->item(0)->tagName;

      $new_tag = $xml->createElementN S( 'http://www.w3.org/1999/XSL/Transform',
      'param' );
      try {
      $xml->insertBefore ( $new_tag, $t_list->item(0) );
      print $xml->saveXML();
      } catch( DomException $e ) {
      print $e->getMessage() ;
      }
      }
      else {
      die( "No tags found" );
      }

      xsl stays the same.
      This still gives "Not found error"

      Is this a bug in the PHP5 DOM implementation?
      [color=blue]
      > "Rutger Claes" <rgc@rgc.tld-of-belgium> wrote in message
      > news:1101891341 .571656@seven.k ulnet.kuleuven. ac.be...[color=green]
      >> Code and problem are from PHP5:
      >>
      >> When I execute the following piece of code, the DomException is thrown
      >> with
      >> a message: Not Found Exception.
      >> I assume this means that the node I extracted from the DomDocument using
      >> getElementsByTa gName() isn't found when I use insertBefore(). I blame
      >> the namespace :-)
      >> When I replace the getElementsByTa gName( 'xsl:template' ), I get the "no
      >> template tag found message" in the else.
      >> What is the NS-safe way to insert a xsl:param tag before the xsl:template
      >> tags?
      >>
      >> <?php
      >>
      >> $xsl = <<<XSL
      >> <?xml version="1.0" ?>
      >> <xsl:styleshe et version="1.0"
      >> xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
      >>
      >> <xsl:import href="xhtml/basic.xhtml.xsl " />
      >> <xsl:import href="xhtml/search.xhtml.xs l" />
      >> <xsl:import href="xhtml/news.xhtml.xsl" />
      >>
      >> <xsl:output
      >> method="xml"
      >> omit-xml-declaration="no "
      >> doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
      >> indent="yes"
      >> media-type="text/html"
      >> />
      >>
      >> <xsl:param name="updatetim e" />
      >>
      >> <xsl:template match="/">
      >> <xsl:apply-imports />
      >> </xsl:template>
      >>
      >> </xsl:stylesheet>
      >> XSL;
      >>
      >> $stylesheet = DomDocument::lo adXML( $xsl );
      >> $temp_list = $stylesheet->getElementsByT agName( 'template' );
      >> if( $temp_list && $temp_list->item(0) ) {
      >> $template = $temp_list->item(0);
      >> $param = $stylesheet->createElemen t( 'xsl:param' );
      >> try {
      >> $stylesheet->insertBefore ( $param, $template );
      >> } catch( DomException $e ) {
      >> print "Exception ".$e->getMessage() ;
      >> print "\n";
      >> print $template->tagName;
      >> }
      >>
      >> }
      >> else {
      >> die( "No template tag found" );
      >> }
      >> ?>
      >>
      >> Thanks in advance.
      >> Rutger Claes
      >> --
      >> Rutger Claes rgc@rgc.tld
      >> Replace tld with top level domain of belgium to contact me
      >> pgp:0x3B7D6BD6
      >> Overflow on /dev/null, please empty the bit bucket.
      >>[/color][/color]

      --
      Rutger Claes rgc@rgc.tld
      Replace tld with top level domain of belgium to contact me pgp:0x3B7D6BD6
      Long computations which yield zero are probably all for naught.

      Comment

      • konsu

        #4
        Re: [PHP5 &amp; DOM] element extracted from doc suddenly not found in insertBefore() -- NS problem

        here is something that does not fail but it shows no output for saveXML. i
        did not spend time trying to figure out why.

        $xml = new DOMDocument;
        $xml->loadXML($xsl );

        $t_list = $xml->getElementsByT agnameNS(
        'http://www.w3.org/1999/XSL/Transform','tem plate' );
        if( $t_list && $t_list->item(0) )
        {
        echo($xml->documentElemen t->tagName);
        $new_tag = $xml->createElementN S(
        'http://www.w3.org/1999/XSL/Transform','par am' );
        try
        {
        $xml->documentElemen t->insertBefore ( $new_tag, $t_list->item(0) );
        echo($xml->saveXML());
        }
        catch( DomException $e )
        {
        echo($e->getMessage() );
        }
        }
        else
        {
        die( "No tags found" );
        }



        "Rutger Claes" <rgc@rgc.tld-of-belgium> wrote in message
        news:1102003188 .895445@seven.k ulnet.kuleuven. ac.be...[color=blue]
        > konsu wrote:
        >[color=green]
        >> there must be a method similar to createElement() which creates an
        >> element
        >> in a given namespace. also check out
        >>[/color]
        > http://us2.php.net/manual/en/functio...ytagnamens.php[color=green]
        >>
        >> konstantin
        >>[/color]
        >
        > If you replace the code with all the ...NS() functions the result is
        > identical. The problem is that when you extract a node from the document
        > and use it to insert another, the node suddenly is not found.
        >
        > New code:
        > $xml = DomDocument::lo adXML( $xsl );
        >
        > $t_list =
        > $xml->getElementsByT agnameNS( 'http://www.w3.org/1999/XSL/Transform',
        > 'template' );
        > if( $t_list && $t_list->item(0) ) {
        > print $t_list->item(0)->tagName;
        >
        > $new_tag = $xml->createElementN S( 'http://www.w3.org/1999/XSL/Transform',
        > 'param' );
        > try {
        > $xml->insertBefore ( $new_tag, $t_list->item(0) );
        > print $xml->saveXML();
        > } catch( DomException $e ) {
        > print $e->getMessage() ;
        > }
        > }
        > else {
        > die( "No tags found" );
        > }
        >
        > xsl stays the same.
        > This still gives "Not found error"
        >
        > Is this a bug in the PHP5 DOM implementation?
        >[color=green]
        >> "Rutger Claes" <rgc@rgc.tld-of-belgium> wrote in message
        >> news:1101891341 .571656@seven.k ulnet.kuleuven. ac.be...[color=darkred]
        >>> Code and problem are from PHP5:
        >>>
        >>> When I execute the following piece of code, the DomException is thrown
        >>> with
        >>> a message: Not Found Exception.
        >>> I assume this means that the node I extracted from the DomDocument using
        >>> getElementsByTa gName() isn't found when I use insertBefore(). I blame
        >>> the namespace :-)
        >>> When I replace the getElementsByTa gName( 'xsl:template' ), I get the "no
        >>> template tag found message" in the else.
        >>> What is the NS-safe way to insert a xsl:param tag before the
        >>> xsl:template
        >>> tags?
        >>>
        >>> <?php
        >>>
        >>> $xsl = <<<XSL
        >>> <?xml version="1.0" ?>
        >>> <xsl:styleshe et version="1.0"
        >>> xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        >>>
        >>> <xsl:import href="xhtml/basic.xhtml.xsl " />
        >>> <xsl:import href="xhtml/search.xhtml.xs l" />
        >>> <xsl:import href="xhtml/news.xhtml.xsl" />
        >>>
        >>> <xsl:output
        >>> method="xml"
        >>> omit-xml-declaration="no "
        >>> doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        >>> indent="yes"
        >>> media-type="text/html"
        >>> />
        >>>
        >>> <xsl:param name="updatetim e" />
        >>>
        >>> <xsl:template match="/">
        >>> <xsl:apply-imports />
        >>> </xsl:template>
        >>>
        >>> </xsl:stylesheet>
        >>> XSL;
        >>>
        >>> $stylesheet = DomDocument::lo adXML( $xsl );
        >>> $temp_list = $stylesheet->getElementsByT agName( 'template' );
        >>> if( $temp_list && $temp_list->item(0) ) {
        >>> $template = $temp_list->item(0);
        >>> $param = $stylesheet->createElemen t( 'xsl:param' );
        >>> try {
        >>> $stylesheet->insertBefore ( $param, $template );
        >>> } catch( DomException $e ) {
        >>> print "Exception ".$e->getMessage() ;
        >>> print "\n";
        >>> print $template->tagName;
        >>> }
        >>>
        >>> }
        >>> else {
        >>> die( "No template tag found" );
        >>> }
        >>> ?>
        >>>
        >>> Thanks in advance.
        >>> Rutger Claes
        >>> --
        >>> Rutger Claes rgc@rgc.tld
        >>> Replace tld with top level domain of belgium to contact me
        >>> pgp:0x3B7D6BD6
        >>> Overflow on /dev/null, please empty the bit bucket.
        >>>[/color][/color]
        >
        > --
        > Rutger Claes rgc@rgc.tld
        > Replace tld with top level domain of belgium to contact me
        > pgp:0x3B7D6BD6
        > Long computations which yield zero are probably all for naught.
        >[/color]


        Comment

        Working...