2012-07-31 3 views
3

xmlValue 내가 그 내가 할 수있는 다음 strsplit에 다른 문자로 유지 (또는 변형 필요한 <br /> 태그를 제거합니다있어서, 내가 문제에 봉착를 제거에서`xmlValue` 방지 여기 는 <br /> 태그

는 예제 :.

를 내가 , recursive=FALSE BU를 시도했습니다

<div class="sl_results_popup_address"> 
1154 S Clark St 
<br/> 
Chicago, IL 60605 
<br/> 
(312) 212-6300 
</div> 

: 그것은 구문 분석 년대 HTML 대

> f <- htmlParse(getForm("http://sites.target.com/site/en/spot/store_locator_popups.jsp", ajax="true", storeNumber=1889), asText=TRUE) 
> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue) 
[1] "1154 S Clark StChicago, IL 60605(312) 212-6300" 

도움이되지 않는다.

줄 바꿈이 <p></p> 인 경우 개별적으로 잡을 수 있기 때문에 더 쉬울 것이지만 <br/>은 실제로 그 방향으로 갈 수 없습니다. 스트라이핑의 수준을 xmlValue (또는 <br/>)이 문서 구문 분석 단계에서 제거되는 것만 줄일 수있는 옵션이 있습니다.

답변

5

두 가지 도움이 될 수 있습니다

app.data<-getForm("http://sites.target.com/site/en/spot/store_locator_popups.jsp", ajax="true", storeNumber=1889) 
app.data<-gsub("<br>","\n",app.data) 
f <- htmlParse(app.data, asText=TRUE) 
out<-xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue) 
> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue) 
[1] "1154 S Clark St\nChicago, IL 60605\n(312) 212-6300" 
> 

그래서 그냥 뭔가 다른과 br 태그를 교체하거나 태그를 유지하려는 경우 원래의 코드와

> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]/text()", xmlValue) 
[1] "1154 S Clark St" "Chicago, IL 60605" "(312) 212-6300" 
> 

를 사용

dum.fun<-function(x){if(xmlName(x)=="br"){"<br/>"}else{xmlValue(x)}} 
xChild<-xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]",xmlChildren) 
lapply(xChild,dum.fun) 
> unlist(lapply(xChild,dum.fun)) 
[1] "1154 S Clark St" "<br/>"    "Chicago, IL 60605" 
[4] "<br/>"    "(312) 212-6300" 
> 
+0

두 가지 멋진 솔루션. 감사. 나는'text()'에 대해 생각하지 않았다. 아직도 xpath에 익숙해 져있다. 언제나처럼 많이 배우기. –

+0

FireBug에서 코드를 복사 했으므로 그 차이가 어디에서 왔는지 모릅니다. –

관련 문제