简体   繁体   English

使用PHP DOM-XPATH解析XML标签,而不会丢失内部标签和数据

[英]Parsing XML tag using PHP DOM - XPATH without losing inside tags and data

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. 我将获得段落的节点,其中包括标签和数据以及<p>标签。

$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.. 标签,但其中不包含<strong>标签。

So my requirement is I need data along with tags inside excluding the <p> tags. 所以我的要求是我需要数据以及除<p>标记之外的其他标记。

Is there any possibility to do that? 有可能这样做吗?

You could simply select the node() children of your p element and iterate the list. 您可以简单地选择p元素的node()子代并迭代列表。 Taking your example expression at face value (although it doesn't match up to your sample input): 以您的示例表达式为面值(尽管它与示例输入不匹配):

//table-wrap-foot[@id='".$tableAttributes."']/p/node()

Note that there are five such nodes: 请注意,有五个这样的节点:

#text 
strong
#text 
br
#text

Even more appropriate would be to select the union of these text and element nodes: 选择这些文本和元素节点的并集更为合适:

//table-wrap-foot[@id='".$tableAttributes."']/p/*|
//table-wrap-foot[@id='".$tableAttributes."']/p/text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM