PHP文档模型:在组成的HTML文档中查找元素 [英] PHP document model: Finding an element in a composed HTML document

查看:69
本文介绍了PHP文档模型:在组成的HTML文档中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用DOM类组成HTML元素的库。我还想使用DOM功能深入了解我的组合元素并检查特定子元素的值。

I have a library that composes HTML elements using the DOM classes. I want to also use the DOM functionality to go deep into my composed elements and check specific child elements for values.

例如,这里的元素由名为 YesNoControl 的类生成:

For example, here's element produced by a class called YesNoControl:

<div id="yes_no">
  <input id="yes_no_1" name="yes_no" value="1" type="radio"/>
  <label for="yes_no_1">Yes</label>
  <input id="yes_no_2" name="yes_no" value="2" type="radio"/>
  <label for="yes_no_2">No</label>
</div>

PHPUnit测试应如下所示:

A PHPUnit test should look something like this:

$element = new YesNoControl();

$element_dom = $element->toDOMDocument(); // This method renders the object to a DOMDocument object.
$element_dom->validate();
$this->assertInstanceOf(\\DOMDocument::class, $element_dom);

$input = $element_dom->getElementById('yes_no_1');
$this->assertInstanceOf(\DOMNode::class, $input); 
$this->assertEquals('1', $input->getAttribute('yes_no_1'));

问题是 $ input 总是出现返回 null

The problem is that $input always comes back null.

在我的研究中,我发现 getElementById()仅在文档具有DOCTYPE HTML标记的情况下有效。在我的构建器中,我以简单的方式创建文档:

In my research, I found that getElementById() works only if the document has a DOCTYPE HTML tag. In my builder, I'm creating the document the plain-and-simple way:

$document = new \DOMDocument();

我试图在这样创建文档后直接将实现标签添加到文档中:

I tried adding the implementation tag to the document directly after creating the document thusly:

$implementation = new \DOMImplementation();
$document->appendChild($implementation->createDocumentType('HTML'));

但是,这会产生带有<!DOCTYPE HTML> 插入错误,例如以下示例:

However, this produces invalid elements with the <!DOCTYPE HTML> incorrectly inserted, like this example:

<div id="yes_no">
  <!DOCTYPE HTML>
  <input id="yes_no_1" name="yes_no" value="1" type="radio"/>
  <label for="yes_no_1">Yes</label>
  <input id="yes_no_2" name="yes_no" value="2" type="radio"/>
  <label for="yes_no_2">No</label>
</div>

我看到了很多答案,其中提到将<!DOCTYPE HTML> 解释为HTML(例如这一个) 。但是我不是从HTML开始的;我正在编写一个元素,然后让DOM库编写HTML。

I see quite a few answers that mention putting <!DOCTYPE HTML> into HTML being interpreted (e.g. this one). But I'm not starting from HTML; I'm composing an element and letting the DOM library write the HTML.

我也尝试过根据此答案即时创建实现

$element_dom = $element->toDOMDocument(); // This method renders the object to a DOMDocument object.
$element_dom->validate();

$dtd = '<!ELEMENT input (#PCDATA)>';
$systemId = 'data://text/plain;base64,'.base64_encode($dtd);
$implementation = new \DOMImplementation;
$element_dom->appendChild($$implementation->createDocumentType('HTML', null, $systemId));

$input = $element_dom->getElementById('yes_no_1');
$this->assertInstanceOf(\DOMNode::class, $input); 
// Failed asserting that null is an instance of class "DOMNode".

我也尝试过使用XPath来获取值:

I've also tried using XPath to get the value:

$xpath = new \DOMXPath($element_dom);
$input = $xpath->query("//*[@id=yes_no_1]")->item(0);
$this->assertInstanceOf(\DOMNode::class, $input); 
// Failed asserting that null is an instance of class "DOMNode".

到目前为止,我没有尝试过。我如何构造测试,以便它可以找到 yes_no_1

So far, nothing I have tried is working. How do I need to structure my test so that it can find yes_no_1?

推荐答案

在您的XPath版本中-您需要用双引号括住要检查的值

In your XPath version - you need quotes round the value your checking for

$input = $xpath->query("//*[@id='yes_no_1']")->item(0);

这篇关于PHP文档模型:在组成的HTML文档中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆