开发者

what is the purpose of DOMDocument->documentURI property?

here is link to documentation: http://php.net/manual/en/class.domdocument.php#domdocument.props.documenturi

But I do not understand is this setting a value that this object reveal, 开发者_JS百科or is this setting that user can change ?

Has this value any effect on html parsing by loadHTML metod ? Can it by used to absolutize all relative links in parsed document ?


Okay, I hope I explain that correctly.

Following is the W3C DOM Interface specification for documentUri:

documentURI of type DOMString, introduced in DOM Level 3

The location of the document or null if undefined or if the Document was created using DOMImplementation.createDocument. No lexical checking is performed when setting this attribute; this could result in a null value returned when using Node.baseURI.

Beware that when the Document supports the feature "HTML" [DOM Level 2 HTML], the href attribute of the HTML BASE element takes precedence over this attribute when computing Node.baseURI.

What does that mean for you?

But I do not understand is this setting a value that this object reveal, or is this setting that user can change ?

It is the URI of the document. If you load a remote URI, like for instance this page, it will contain the remote URI, e.g. the URL currently shown in your browser address bar. The value is public, so it is writable.

Has this value any effect on html parsing by loadHTML metod?

In theory, yes. Practically, it depends on if your DOMImplementation has the HTML 2.0 feature.

Can it by used to absolutize all relative links in parsed document?

Not automatically. But you can very much use it to prepend it manually to any links starting with a path. Of course you need to implement the logic to check whether the href value needs to be expanded yourself.


The DOMDocuemnt::$documentURI property is well explained in the PHP manual:

The location of the document or NULL if undefined.

It is a public property that is set if you load the document from a location. This is normally the filename (e.g. "file:///C:/Tests/dom/data/file1.xml") or the URI ("data://text/html;encoding=base64,PHA+aGVsbG8gd29ybGQ8L3A+") used in DOMDocument::load() or DOMDocument::loadHTMLFile() respectively.

If you load an XML string (DOMDocument::loadXML()) then the documentURI is the current working directory.

If you load an HTML string (DOMDocument::loadHTML()) then the documentURI is NULL and it does not matter if there is a <base href=""> element in that HTML or not.

Examples:

<?php
/**
 * what is the purpose of DOMDocument->documentURI property?
 * @link https://stackoverflow.com/q/4003543/367456
 */

$doc = new DOMDocument();

$doc->load(__DIR__ . '/data/file1.xml');
var_dump($doc->documentURI); # "file:///C:/Tests/dom/data/file1.xml"

$doc->loadHTMLFile(__DIR__ . '/data/file1.html');
var_dump($doc->documentURI); # "file:///C:/Tests/dom/data/file1.html"

$doc->loadXML('<p>hello world</p>');
var_dump($doc->documentURI); # "file:///C:/Tests/dom/"  (current working directory)

$doc->loadHTML('<p>hello world</p>');
var_dump($doc->documentURI); # NULL

$doc->loadHTML('<base href="http://example.com/base/"><i>test</i>');
var_dump($doc->documentURI); # NULL

$doc->loadHTMLFile('data://text/html;encoding=base64,' . base64_encode('<p>hello world</p>'));
var_dump($doc->documentURI); # "data://text/html;encoding=base64,PHA+aGVsbG8gd29ybGQ8L3A+"

Caution: This property might have been modeled after DOM Core Level 3.0 specification (in conjunction with DOMNode::$baseUri), however that DOM Core Level (a so called Feature Version) is not supported by PHP's DOMDocument.

This property can be used to set / resolve the base URI of a HTML document. If it's NULL or an empty string, you need to provide it your own. For an example resolving links in/of the document, see problem with adding root path using php domdocument for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜