spy*_*g63 5 php xml xslt xpath simplexml
如何选择以下节点的字符串内容:
<span class="url">
word
<b class=" ">test</b>
</span>
<span class="url">
word
<b class=" ">test2</b>
more words
</span>
Run Code Online (Sandbox Code Playgroud)
我尝试过一些事情
//span/text()
Run Code Online (Sandbox Code Playgroud)
没有获得粗体标签
//span/string(.)
Run Code Online (Sandbox Code Playgroud)
是无效的
string(//span)
Run Code Online (Sandbox Code Playgroud)
只选择1个节点
我在 php 中使用 simple_xml ,我认为唯一的其他选择是使用 //span ,它返回:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)
[b] => test
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)
[b] => test2
)
)
Run Code Online (Sandbox Code Playgroud)
*请注意,它还会从第二个跨度中删除“更多单词”文本。
所以我想我可以如何使用 php 来压平数组中的项目?Xpath 是首选,但任何其他想法也会有所帮助。
$xml = '<foo>
<span class="url">
word
<b class=" ">test</b>
</span>
<span class="url">
word
<b class=" ">test2</b>
more words
</span>
</foo>';
$dom = new DOMDocument();
$dom->loadXML($xml); //or load an HTML document with loadHTML()
$x= new DOMXpath($dom);
foreach($x->query("//span[@class='url']") as $node) echo $node->textContent;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14289 次 |
| 最近记录: |