コメントのparentNodeを属性(IDやクラス)で出力したい
このhtmlコードがある場合
<div><span><!--test--></span><div class="myclass"><!--test_comment--></div></div>
次の出力が欲しい
<div class="myclass"><!--test_comment--></div>
このhtmlコードがある場合
<div><span id="myid" style="color:blue;font-weight:bold"><!--test_comment--></span><div class="myclass"><!--test--></div></div>
次の出力が欲しい
<span id="myid" style="color:blue;font-weight:bold"><!--test_comment--></span>
これは、xpath を使用した私の php DOM コードです
<?php
$html = <<<STR
<div><span><!--test--></span><div class="myclass"><!--test_comment--></div></div>
STR;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$node = $xpath->query('//comment()');
$myComment = $node->item(1)->textContent; // <!--test_comment-->
$myNode = $node->item(1)->parentNode->nodeName; // div
echo $node->item(1)->parentNode->nodeValue; //NOT WORKING
echo "<" . $myNode . ">" . $myComment . "</" . $myNode . ">";
?>
問題は、特定のコメント () を検索する方法がわからないことです。似たようなものが欲しい$xpath->query('//comment() == "test_comment"');
私のphpコードは
<div>test_comment</div>
div 内の属性 (クラスまたは ID) を取得するにはどうすればよいですか? nodeValue が機能していません。
私が初心者であるとき、私のコードに関する他のコメントも歓迎します