简体   繁体   English

如何交叉引用锚标签?

[英]how to cross reference of anchor tag?

XHTML: XHTML:

<root>
<a href="1#fn1" class="fn-ref" id="s9781473910270.i11"><sup>1</sup></a>
<p>some text<a href="1#fnref1" id="s9781473910270.i237">↩</a></p>
</root>

From the above sample, if href of both anchor are same (like 1#fn1 and 1#fnref1 ) then have to exchange their ID's as their href and have to wrap the anchor tag(which has fnref in their href in it) with <span class="label-fn"> then need to set class="ref-fn-ref" for that anchor tag. 从上面的示例中,如果href两个锚的是相同的(如1#fn11#fnref1 )然后必须交换它们的ID's作为其href并具有包裹锚定标记(已经fnref在他们的href在它)与<span class="label-fn">然后需要为该锚标记设置class="ref-fn-ref"

Expected output: 预期产量:

<root>
<a href="#s9781473910270.i237" class="fn-ref" id="s9781473910270.i11"><sup>1</sup>
</a>
<p>sometext<span class="label-fn"><a href="#s9781473910270.i11" class="ref-fn-ref" 
id="s9781473910270.i237">↩</a></span></p>
</root>

I have tried so far, 到目前为止,我已经尝试过

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile("sample.xhtml", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");
$className="fn-ref";
$anchor1 = $xp->query("//[contains(@class, '$className')]");
//????

I stucked in between, I don't know how to get the array of another anchor tags matched with anchor1 array. 我陷入之间,我不知道如何获取与anchor1数组匹配的另一个锚标签的数组。

Figured out myself to get my output. 弄清楚自己要得到我的输出。

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");
$a1 = $xp->query("//*[contains(@class, 'fn-ref')]");
$a2 = $xp->query("//*[contains(@href, '#fnref')]");

foreach($a1 as $a1v) {
    $id1=$a1v->getAttribute('id');
    preg_match("/([\\d]+)#fn([\\d+])/s",$a1v->getAttribute('href'),$m1);
    $href1=$m1[1]."#fnref".$m1[2];  
    foreach($a2 as $a2v) {
    if($a2v->getAttribute('href')===$href1){
        $id2=$a2v->getAttribute('id');
        $a2v->setAttribute('class','ref-fn-ref');
        $a2v->setAttribute('href',"#".$id1);
        $a1v->setAttribute('href',"#".$id2);
        break;
    }
}
}
$r=$dom->saveXML();

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

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