Problem with Adding Root Path Using PHP Domdocument

problem with adding root path using php domdocument

I see some problems in your code:

  1. The decision whether or not an URI has a full root path (is a fully qualified URI) or not.
  2. You're not resolving relative URLs to the base URL. Just appending does not do the job.
  3. The function returns a DomDocument Object and not a string. I assume you don't want that but I don't know, you have not written in your question.

How to detect if a URL is a relative one.

Relative URLs don't specifiy a protocol. So I would check for that to determine whether or not a href attribute is a fully qualified (absolute) URI or not (Demo):

$isRelative = (bool) !parse_url($url, PHP_URL_SCHEME);

Resolving a relative URL to a base URL

However this won't help you to properly resolve a relative URL to the base URL. What you do is conceptually broken. It's specified in an RFC how to resolve a relative URI to the base URL (RFC 1808 and RFC 3986). You can use an existing library to just let the work do for you, a working one is Net_URL2:

require_once('Net/URL2.php'); # or configure your autoloader

$baseUrl = 'http://www.example.com/test/images.html';

$hrefRelativeOrAbsolute = '...';

$baseUrl = new Net_URL2($baseUrl);

$urlAbsolute = (string) $baseUrl->resolve($hrefRelativeOrAbsolute);

Problem with iterating throught DOM using DOMDocument

I think the main problem is that when you use getElementsByTagName(), this returns a list of nodes (actually a DOMNodeList). So when you want to access (for example) the first item for that tag, you will need to reference the first item in an array.

If you extended your initial code to get the nested tag elements, you could end up with the following code, which always uses [0] on the result of getElementsByTagName() to pick out the first item.

$title = $doc->getElementById('info')->childNodes->item(1)->nodeValue;
$volume_list = $doc->getElementById('list')->getElementsByTagName('dl');

$a = $volume_list[0]->getElementsByTagName('dd')[0]->getElementsByTagName('a');

echo $a[0]->getAttribute('href');

Rest api: having trouble with root path directory

You are using relative path and because database.php and functions.php are in the includes folder, you can just remove the includes part in the path.

i.e:

database.php
<?php
session_start();
require_once('functions.php');
?>
functions.php
<?php require_once('database.php');?>

Another way to do this is to use absolute path when your include/require (I prefer this way)

i.e:

database.php
<?php
session_start();
require_once(__DIR__ . '/../includes/functions.php');
?>

How to change root of a node with DOMDocument methods?

As this has not been really answered yet, the error you get about not found is because of a little error in the renameNode() function you've copied.

In a somewhat related question about renaming different elements in the DOM I've seen this problem as well and used an adoption of that function in my answer that does not have this error:

/**
* Renames a node in a DOM Document.
*
* @param DOMElement $node
* @param string $name
*
* @return DOMNode
*/
function dom_rename_element(DOMElement $node, $name) {
$renamed = $node->ownerDocument->createElement($name);

foreach ($node->attributes as $attribute) {
$renamed->setAttribute($attribute->nodeName, $attribute->nodeValue);
}

while ($node->firstChild) {
$renamed->appendChild($node->firstChild);
}

return $node->parentNode->replaceChild($renamed, $node);
}

You might have spotted it in the last line of the function body: This is using ->parentNode instead of ->ownerDocument. As $node was not a child of the document, you did get the error. And it also was wrong to assume that it should be. Instead use the parent element to search for the child in there to replace it ;)

This has not been outlined in the PHP manual usernotes so far, however, if you did follow the link to the blog-post that originally suggested the renameNode() function you could find a comment below it offering this solution as well.

Anyway, my variant here uses a slightly different variable naming and is more distinct about the types. Like the example in the PHP manual it misses the variant that deals with namespace nodes. I'm not yet booked what would be best, e.g. creating an additional function dealing with it, taking over namespace from the node to rename or changing the namespace explicitly in a different function.

Attribute not creating using DomDocument in PHP

To set an attribute on a node:

$resource1->setAttribute('type', 'webcontent');

To set a namespaced attribute on a node (assuming this is the namespace represented by the "adlcp" prefix):

$resource1->setAttributeNS('http://www.adlnet.org/xsd/adlcp_rootv1p2', 'adlcp:scormType', 'sco');

PHP DOMDocument Writing Sitemap Problems - Uncaught exception 'DOMException'

Your call to createElement is completely bogus. You can't add attributes that way. Try this:

<?php
$xml = new DOMDocument("1.0", "UTF-8");
$xml_urlset = $xml->createElement('urlset');
$xml_urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml_urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml_urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
$xml_url = $xml->createElement("url","this text");
$xml_urlset->appendChild($xml_url);
$xml->appendChild($xml_urlset);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->save("test.xml");
?>

@fopen in PHP does not support relative URLs

Why does PHP not support relative URLs?

Relative URL support needs a Base URL the relative URL is relative to. Please see 4.2. Relative Reference in RFC 3986 URI Generic Syntax.

Taken that into account, there is a PHP Library available that actually does support relative URLs and it is compatible with fopen. It's called Net_URL2:

require_once('Net/URL2.php'); # or configure your autoloader

$baseUrl = 'http://cdn.domain.net';

$hrefRelativeOrAbsolute = '//cdn.domain.net';

$baseUrl = new Net_URL2($baseUrl);

$urlAbsolute = (string) $baseUrl->resolve($hrefRelativeOrAbsolute);

See as well problem with adding root path using php domdocument.



Related Topics



Leave a reply



Submit