У нас вы можете посмотреть бесплатно Why XPath Queries for Tags Fail in Dom\HTMLDocument and How to Fix Them или скачать в максимальном доступном качестве, видео которое было загружено на ютуб. Для загрузки выберите вариант из формы ниже:
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием видео, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса ClipSaver.ru
Learn why XPath queries for specific tags like div return no results in PHP 8.4's Dom\HTMLDocument, and how to correctly handle namespaces or disable them. --- This video is based on the question https://stackoverflow.com/q/79443721/ asked by the user 'Robo Robok' ( https://stackoverflow.com/u/4403732/ ) and on the answer https://stackoverflow.com/a/79443754/ provided by the user 'phihag' ( https://stackoverflow.com/u/35070/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why can't I search for tags with Dom\HTMLDocument? Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to drop me a comment under this video. --- Introduction PHP 8.4 introduced the Dom\HTMLDocument class for better HTML parsing. However, many developers face confusion when XPath queries targeting specific elements (e.g., //div) return empty results despite the elements existing. The Problem: Namespace in Dom\HTMLDocument When you parse HTML with Dom\HTMLDocument::createFromString(), all elements are automatically placed inside the XHTML namespace: Namespace URI: http://www.w3.org/1999/xhtml This means that XPath queries like //div (which looks for un-namespaced tags) won't match these nodes. Interestingly, the wildcard selector //* will still return all elements regardless of namespace, which is why you see elements but can't find tag-specific ones. Understanding XPath with Namespaces XPath requires you to explicitly specify namespaces when querying namespaced elements. For example: [[See Video to Reveal this Text or Code Snippet]] This will correctly retrieve all <div> elements because it matches the namespace. The Simple Fix: Disable Default Namespace In most HTML use cases, you don't want to process documents with namespace awareness. Luckily, PHP provides a way to disable the default XHTML namespace during parsing: [[See Video to Reveal this Text or Code Snippet]] By passing the Dom\HTML_NO_DEFAULT_NS flag, elements are created without a namespace, letting your XPath queries work naturally. Summary Dom\HTMLDocument::createFromString() defaults to the XHTML namespace. XPath queries for un-namespaced tags won't match these elements. Registering the XHTML namespace in XPath or disabling the default namespace fixes the issue. For typical HTML parsing, use Dom\HTML_NO_DEFAULT_NS for smoother XPath queries. This ensures your queries for tags like <div>, <span>, etc., work as expected without extra namespace handling.