2012-06-28 7 views
6

Wie Text nach den br-Tags in den folgenden Zeilen extrahieren:XPath zu extrahieren Text nach br Tags in R

<div id='population'> 
    The Snow Leopard Survival Strategy (McCarthy <em>et al.</em> 2003, Table 
    II) compiled national snow leopard population estimates, updating the work 
    of Fox (1994). Many of the estimates are acknowledged to be rough and out 
    of date, but the total estimated population is 4,080-6,590, as follows:<br> 
    <br> 
    Afghanistan: 100-200?<br> 
    Bhutan: 100-200?<br> 
    China: 2,000-2,500<br> 
    India: 200-600<br> 
    Kazakhstan: 180-200<br> 
    Kyrgyzstan: 150-500<br> 
    Mongolia: 500-1,000<br> 
    Nepal: 300-500<br> 
    Pakistan: 200-420<br> 
    Russia: 150-200<br> 
    Tajikistan: 180-220<br> 
    Uzbekistan: 20-50 
</div> 

Ich habe so weit wie:

xpathSApply(h, '//div[@id="population"]', xmlValue) 

aber ich bin fest jetzt ...

+3

Es gibt keinen Text _within_ '
' Tags ... Meinst du _between_ die verschiedenen Tags? Vielleicht möchten Sie klarstellen, was Sie _exactly_ wollen. Eine erste Schätzung wäre ''div [@ id =" Bevölkerung "]/text() [vorhergehendes Geschwister :: br]' – Wrikken

+0

Das ist es! Setzen Sie es als Antwort, und ich werde es überprüfen – Kay

Antwort

18

Es hilft, wenn Sie erkennen, dass Text auch ein Knoten ist.

//div[@id="population"]/text()[preceding-sibling::br] 

Technisch zwischen<br/> Tags würde bedeuten:

//div[@id="population"]/text()[preceding-sibling::br and following-sibling::br] 

... aber ich denke, das ist nicht das, was Sie wollen, dass alle Texte in den div als <br/> ‚folgt s kann abgerufen werden An diesem Punkt.

+0

Ich habe das 'zwischen' bearbeitet .. Vielen Dank! – Kay

Verwandte Themen