Frage

I have an xpath query:

$q = $xpath->query("//p[@id='v{$versenumber}']/following-sibling::div[@class='admonition']");

Which works fine, very well in fact. And I have using the following to extract the HTML I receive from it:

$saveHTML = $dom->saveHTML($q->item(0));

However, Inside this query I have HREFS that I want to replace with something else. I am having trouble actually recognising the hrefs. I thought about having another query which would be the same, but with /a at the end, but that didn't return anything.

I would have thought I could access them like this:

$x = $q->item(0)->getElementByTagName('a');

But that doesnt seem to work either :( what am I doing wrong?

update

HTML I want to parse:

<p id="v1"><span class="verseref">1</span></p>
<div class="notes">
<p class="first">Notes</p>
<p class="last">Paragraph</p>
</div>
<div class="admonition">
<p class="last">HTML with <a href='foobar'>inside it</a>.  I want to get all href attributes from here.</p>
</div>

And using the above query, I can get the text fine, it's just that I want to deal with each 'href' attribute as they are wrong, and I need to change them. So I deal with each <div class'admonition'> individually, and all the hrefs inside them.

However using:

$q = $xpath->query("//p[@id='v{$versenumber}']/following-sibling::div[@class='admonition']//a/@href");

I seem to get a huge amount of href's for one paragraph where there is only one.

../../ga/ch1/#v1
#v6
#v5
#v6
../../mr/ch16/#v20
../ch12/
../../heb/ch13/#v9
../ch12/
../ch3/#v1
../../lu/ch1/#v6
../../1jo/ch1/#v8
../../1jo/ch1/#v10
../../1jo/ch1/#v7
../../1jo/ch1/#v9
#v1
../../eph/ch4/#v13
../../ro/ch14/
../../ro/ch14/#v1
../ch5/
../ch6/
../ch7/
../ch8/
../ch11/
../ch12/
../ch15/
../../ro/ch14/
#v12
../ch3/#v4
../ch15/#v24
../../eph/ch5/#v17
../../ro/ch8/#v6
../../../ot/ge/ch11/#v3
../../../ot/ps/ch133/
../../../ot/jer/ch32/#v39
../../ac/ch4/#v32
../../ro/ch12/#v16
../../ro/ch15/#v5
../../php/ch1/#v27
../../php/ch2/#v1
../../1th/ch5/#v13
../../jas/ch3/#v13
../../1pe/ch3/#v8
../../eph/ch4/#v13
../ch16/#v15
../ch16/#v17
../ch16/#v24
../../ac/ch18/#v12
../ch16/#v15
../ch16/#v17
../../ac/ch11/#v18
../../mt/ch28/#v19
../../mt/ch26/#v2
../ch2/#v14
../../ro/ch1/#v16
../../ro/ch1/#v16
../../2co/ch4/#v3
#v17
../../ac/ch20/#v30
#v18
../../../ot/isa/ch29/#v14
../../../ot/isa/ch29/#v14
../../../ot/isa/ch29/#v13
../ch2/#v14
../../ro/ch10/#v10
#v21
#v26
../ch2/
#v18
#v11
../../lu/ch6/#v38
../../../ot/ps/ch14/#v1
../../../ot/ps/ch53/#v1
../../col/ch2/#v3
#v23
#v18
../../ac/ch5/#v34
../../ac/ch26/#v24
../../ga/ch2/#v1
#v26
#v25
../../ac/ch24/#v25
../../2co/ch10/#v12
../../ro/ch7/#v18
#v30
../../ro/ch7/#v18
../../joh/ch8/#v44
../../mt/ch26/#v41
../../ro/ch8/#v18
#v26
../../../ot/isa/ch42/#v8
../../joh/ch3/#v3
../../../ot/pr/ch3/#v6
../../ro/ch8/#v23
#v26

..Which must be the entire document, otherwise I don't know where it is getting all these hrefs from.

War es hilfreich?

Lösung

following-sibling is an axis, not a selector, it just specifies the mode of navigation through the DOM. Your following-sibling::div[@class='admonition'] asks for all the "admonition" divs that follow (at any distance) the selected p. The position() function should help you solve this. Try something like following-sibling::div[@class='admonition' and position()=1].

Andere Tipps

$a_tags = $xpath->query('.//a', $q->item(0));

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top