使用 PHP DOM - XPATH 解析 XML 标签而不丢失内部标签和数据
我想解析类似于以下内容的数据:
<table-wrap id ="T1">
<table-wrap-foot>
<fn>
<p>
Blah blah blah <strong>dsf</strong> blah blah blah <br>
</p>
</fn>
<table-wrap-foot>
<table-wrap>
当我调用时,
$x = $xpath->query("//table-wrap-foot[@id='" . $tableAttributes . "']/p")->item(0);
我将获取段落的节点,包括内部的标签和数据以及
标签。
$x = $xpath->query("//table-wrap-foot[@id='".$tableAttributes."']/p")->item(0)->nodeValue;
我将获取标签内的数据
,但它不包含 标签。
所以我的要求是我需要数据以及内部标签,不包括 < /代码> 标签。
有可能这样做吗?
I want to parse data which looks similar to the following:
<table-wrap id ="T1">
<table-wrap-foot>
<fn>
<p>
Blah blah blah <strong>dsf</strong> blah blah blah <br>
</p>
</fn>
<table-wrap-foot>
<table-wrap>
When I call
$x = $xpath->query("//table-wrap-foot[@id='" . $tableAttributes . "']/p")->item(0);
I'll get the node of paragraph including tags and data inside along with the <p>
tags.
$x = $xpath->query("//table-wrap-foot[@id='".$tableAttributes."']/p")->item(0)->nodeValue;
I'll get the data inside the
tags but it doesn't contain <strong>
tag..
So my requirement is I need data along with tags inside excluding the <p>
tags.
Is there any possibility to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需选择
p
元素的node()
子元素并迭代列表即可。以表面值为例(尽管它与示例输入不匹配):请注意,有五个这样的节点:
更合适的是选择这些文本和元素节点的并集:
You could simply select the
node()
children of yourp
element and iterate the list. Taking your example expression at face value (although it doesn't match up to your sample input):Note that there are five such nodes:
Even more appropriate would be to select the union of these text and element nodes: