php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16647 DomDocument->get_element_by_id() doesn't work
Submitted: 2002-04-16 20:04 UTC Modified: 2002-05-16 10:28 UTC
From: s dot li at gmx dot de Assigned:
Status: Closed Package: DOM XML related
PHP Version: 4.0CVS-2002-04-1 OS: linux 2.4.4
Private report: No CVE-ID: None
 [2002-04-16 20:04 UTC] s dot li at gmx dot de
The following script returns bool(false):
<?php

$src = <<< _END
<html>
<head><title> Test </title></head>
<body> 
<h1>Test</h1>
<span id="test">Foo</span>
</body>
</html>
_END;

$doc = domxml_open_mem($src);

$n = $doc->get_element_by_id("test");
var_dump($n);

?>

Workaround: Use Xpath expressions to find the node:
<?php

$src = <<< _END
<html>
<head><title> Test </title></head>
<body> 
<h1>Test</h1>
<span id="test">Foo</span>
</body>
</html>
_END;

$doc = domxml_open_mem($src);

$ctx = $doc->xpath_new_context();
$res = $ctx->xpath_eval("//*[@id='test']");
$n = $res->nodeset[0];
var_dump($n);

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-18 01:37 UTC] steinm@php.net
get_element_by_id() used xpath_eval as well but
searches for "//*[@ID = '%s']". If you capitalize
the id it should work. Do you have an idea how to search
case insensitve?
 [2002-04-18 07:44 UTC] chregu@php.net
Ooop. didn't much think about case sensitivity back then :)

A quick soultion would be:

"//*[@ID = '%s' or @id = '%s']".

I don't know, which version is really correct according to W3C (but anyways, the ID-attribute should be stated in the DTD, so this whole xpath-approach is actually rather wrong :) )

chregu
 [2002-04-18 10:10 UTC] s dot li at gmx dot de
Quote from the Document Object Model (DOM) Level 2 Core Specification:
  Attributes with the name "ID" are not of type ID unless 
  so defined. Implementations that do not know whether
  attributes are of type ID or not are expected to return
  null. 

That means that DomDocument->get_document_by_id("X") and XPathContext->xpath_eval("//*[@id='X']) are not equivalent and that the former function should only return a result if a DTD is referenced which declares attributes of type ID.
 [2002-05-16 10:28 UTC] chregu@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 19:01:32 2024 UTC