使用 DOM 和 xpath 设置无样式链接的样式
对于我正在构建的系统,我定义了一个存储在 LINKSTYLE
中的通用 style
,它应该应用于尚未设置样式的 a
元素(内联) )。我对 DOMDocument 或 xpath 不太有经验,我不知道出了什么问题。
感谢戈登,我更新了我的代码:
libxml_use_internal_errors(true);
$html = '<a href="#">test</a>'.
'<a href="#" style="border:1px solid #000;">test2</a>';
$dom = new DOMDocument();
$dom->loadHtml($html);
$dom->normalizeDocument();
$xpath = new DOMXPath($dom);
foreach($xpath->query('//a[not(@style)]') as $node)
$node->setAttribute('style','border:1px solid #000');
return $html;
使用这个更新代码,我不再收到错误,但是 a
元素没有设置样式。
For a system I am building I am defining a general style
stored in LINKSTYLE
that should be applied to a
elements that are not yet styled (inline). I am not very experienced with the DOMDocument
or xpath
and I can't figure out what is going wrong.
Thanks to Gordon I've updated my code:
libxml_use_internal_errors(true);
$html = '<a href="#">test</a>'.
'<a href="#" style="border:1px solid #000;">test2</a>';
$dom = new DOMDocument();
$dom->loadHtml($html);
$dom->normalizeDocument();
$xpath = new DOMXPath($dom);
foreach($xpath->query('//a[not(@style)]') as $node)
$node->setAttribute('style','border:1px solid #000');
return $html;
With this updated code I receive no more errors, however the a
element does not get styled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
libxml_use_internal_errors(true)
来抑制由loadHTML
产生的解析错误。libxml_use_internal_errors()
— 禁用 libxml 错误并允许用户获取错误信息XPath 查询无效,因为
contains
需要在 style 属性中搜索一个值。fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
如果您想查找没有样式元素的所有锚点,只需使用
您没有看到您的更改,因为您正在返回存储在 $html 中的字符串。使用 DOMDocument 加载字符串后,必须在运行查询并修改 DOMDocument 该字符串的内部表示后将其序列化回来。
示例(演示)
输出:
注意为了将
saveHTML
与参数一起使用,你至少需要PHP 5.3.6。Use
libxml_use_internal_errors(true)
to suppress parsing errors stemming fromloadHTML
.libxml_use_internal_errors()
— Disable libxml errors and allow user to fetch error informationThe XPath query is invalid because
contains
expects a value to search for in the style attribute.fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
If you want to find all anchors without a style element, just use
You are not seeing your changes, because you are returning the string stored in $html. Once you loaded the string with DOMDocument, you have to serialize it back after you have have run your query and modified the DOMDocument's internal representation of that string.
Example (demo)
Output:
Note that in order to use
saveHTML
with an argument, you need at least PHP 5.3.6.当您将文档内部
&
用于创建实体引用以外的其他目的(例如"
)时,会发生第一个错误(编辑之前)。当您分隔 GET 参数时,通常会在 URL 中发生这种情况。
您可以使用 Gordon 的建议忽略此错误或修复它(用
&
替换&
的出现)。The first error (before editing) occurs when you use inside document a
&
for other purposes than creating a entity-reference (e.g."
).Usually this happens in URLs when you delimit GET-parameters.
You can ignore this errors using Gordon's suggestion or fix it(replace occurences of
&
by&
).我想知道是否可以更明智地解决这个问题,例如使用选择器。在 CSS3 中,只能处理那些没有
style
属性的标记:
因此,如果您的文档已经有样式表,则可以轻松添加它。
如果没有,则必须将
添加到文档中。这也可以使用 DomDocument 来完成,但我发现它有点复杂。不过,我让它发挥了一些作用:
示例输出:
如果 CSS 与其他更高的选择器发生冲突,这不是一个简单的解决方案。
!important
可能会有所帮助。HTML 片段
至于获取更改后的 HTML 片段,这是一些可以与 gordons 建议一起使用的附加代码。只是body标签的inner-html,这次我玩了一下SPL:
foreach肯定更具可读性和内存友好性:
I was wondering if it's possible to solve this more CCS-wise, e.g. with a selector. In CSS3 it's possible to only address those
<a>
tags that don't have thestyle
attribute:So if your documents already have a stylesheet it could be easily added.
If not, then a
<style>
must be added to the document. This can be done with DomDocument as well but I found it a bit complicated. However I got it to work for some little play:Example output:
If the CSS clashes with other, higher selectors, this is not an easy solution.
!important
might help though.HTML Fragment
And as far of getting the changed HTML fragment, this is some additional code that can work with gordons suggestion. Just the inner-html of the body tag, this time I played a bit with the SPL:
A foreach is definitely more readable and memory friendly: