【问题标题】:how to cross reference of anchor tag?如何交叉引用锚标签?
【发布时间】:2015-10-10 11:20:22
【问题描述】:

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>

从上面的示例中,如果两个锚点的href 相同(如1#fn11#fnref1),则必须将它们的ID's 交换为它们的href 并且必须包装锚标记(它有fnref 在他们的href 中)和&lt;span class="label-fn"&gt; 然后需要为该锚标记设置class="ref-fn-ref"

预期输出:

<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>

到目前为止我已经尝试过了,

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')]");
//????

我夹在中间,我不知道如何获取与 anchor1 数组匹配的另一个锚标签的数组。

【问题讨论】:

    标签: php dom xpath xhtml


    【解决方案1】:

    自己想办法得到我的输出。

    $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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多