2017-02-14 1 views
1

--update-- 어쩌면 누군가가 또 다른 가능성은 매우 R에 .docx를 가져 오기의 장으로 모든

먼저 .docx 문서를 분할 가정 할 수있다, 내가 원하는 장을 .XML 같은 복잡한 .DOCX 파일을 가져 및 추출 이 멋진 포럼에 감사드립니다. 다가올 이슈에 대한 몇 가지 해결책을 찾았습니다. 하지만 이번에는 아무 것도 발견하지 못했습니다 ... 으로 포맷 된 색인을 포함하는 복잡한 .docx 문서가 있습니다.

library(XML) 
xmlfile <- xmlParse("C:/Users/Documents/stihl.xml", options = HUGE) 

topxml <- xmlRoot(xmlfile) 

topxml <- xmlSApply(topxml, function(x) xmlSApply(x, xmlValue)) 
xml_df <- data.frame(t(topxml), row.names = NULL, node) 

그리고 다른 가능성은 XML 파일을 읽는 것입니다. 내 .docx 문서에 색인이 있고 이제 여러 색인 콘텐츠를 추출하고 싶습니다. 결과로 .docx 예를

1. Introduction 
    This is an introduction importing XML by R. 
2. UserGuide 
    Userguides are often helpful. 
2.1 Style 
    The style should be always the same. 
2.2 Language 
    I hope my Language is readable, because I'm contacting you from Germany. 

로는 벡터에 저장 예를 들어, 구분 챕터의 내용을받을 것이 좋을 것이다.

result 
[1]This is an introduction importing XML by R. 
[2]Userguides are often helpful. 
[3]The style should be always the same. 
[4]I hope my Language is readable, because I'm contacting you from Germany. 

아마도 구조를 유지하는 다른 방법이있을 수 있지만 가장 쉬운 방법으로 트리 구조가 포함 된 XML 가져 오기를 언급했습니다.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<?mso-application progid="Word.Document"?> 
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"> 

    <pkg:part 
    pkg:name="/_rels/.rels" 
    pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" 
    pkg:padding="512"> 
    <pkg:xmlData> 
     <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> 
      <Relationship 
      Id="rId3" 
      Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" 
      Target="docProps/app.xml"/> 
      <Relationship 
      Id="rId2" 
      Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" 
      Target="docProps/core.xml"/> 
      <Relationship Id="rId1" 
      Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" 
      Target="word/document.xml"/> 
     </Relationships> 
    </pkg:xmlData> 
    </pkg:part> 

    <pkg:part 
    #serveral relationships 
    </pkg:part> 

    <pkg:part 
    pkg:name="/word/document.xml" 
    pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"> 
    <pkg:xmlData> 

     <w:document mc:Ignorable="w14 w15 wp14" 




    xmlns:wpc:http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas 
    xmlns:mc:http://schemas.openxmlformats.org/markup-compatibility/2006 
    xmlns:o:urn:schemas-microsoft-com:office:office 
    xmlns:r:http://schemas.openxmlformats.org/officeDocument/2006/relationships 
    xmlns:m:http://schemas.openxmlformats.org/officeDocument/2006/math 
    xmlns:v:urn:schemas-microsoft-com:vml 
    xmlns:wp14:http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing 
    xmlns:wp:http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing 
    xmlns:w10:urn:schemas-microsoft-com:office:word 
    xmlns:w:http://schemas.openxmlformats.org/wordprocessingml/2006/main 
    xmlns:w14:http://schemas.microsoft.com/office/word/2010/wordml 
    xmlns:w15:http://schemas.microsoft.com/office/word/2012/wordml 
    xmlns:wpg:http://schemas.microsoft.com/office/word/2010/wordprocessingGroup 
    xmlns:wpi:http://schemas.microsoft.com/office/word/2010/wordprocessingInk 
    xmlns:wne:http://schemas.microsoft.com/office/word/2006/wordml 
    xmlns:wps:http://schemas.microsoft.com/office/word/2010/wordprocessingShape 

     <w:body> 

      <w:p> ... 
      </w:p> 

      <w:p w14:paraId="5BB64FEF" w14:textId="77777777" w:rsidR="005A3789" w:rsidRDefault="005A3789" w:rsidP="005A3789"> 
      <w:pPr> 
      <w:pStyle w:val="Inhaltsverzeichnisberschrift"/> 
      </w:pPr> 
      <w:r> 
      <w:lastRenderedPageBreak/> 
      <w:t>Inhaltsverzeichnis</w:t> 
      </w:r> 
      </w:p> 

'Inhaltsverzeichnis 리뷰는'내 인덱스의 titel입니다. > 3.part - -> XMLDATA -> 문서 -> 본체 - 경로 패키지 정보는, 예를 들어 여기에 저장된

> P

<w:p w14:paraId="15ECF978" w14:textId="77777777" w:rsidR="009B5500" w:rsidRDefault="005A3789"> 
<w:pPr> 
<w:pStyle w:val="Verzeichnis1"/> 
<w:rPr> 
<w:rFonts w:eastAsiaTheme="minorEastAsia"/> 
<w:b w:val="0"/> 
<w:noProof/> 
<w:color w:val="auto"/> 
<w:lang w:eastAsia="de-DE"/> 
</w:rPr> 
</w:pPr> 
<w:r> 
<w:rPr> 
<w:b w:val="0"/> 
</w:rPr> 
<w:fldChar w:fldCharType="begin"/> 
</w:r> 
<w:r> 
<w:instrText xml:space="preserve"> TOC \o "1-4" \h \z \u 
</w:instrText> 
</w:r> 
<w:r> 
<w:rPr> 
<w:b w:val="0"/> 
</w:rPr> 
<w:fldChar w:fldCharType="separate"/> 
</w:r> 
<w:hyperlink w:anchor="_Toc474825312" w:history="1"> 
<w:r w:rsidR="009B5500" w:rsidRPr="009D0220"><w:rPr> 
<w:rStyle w:val="Hyperlink"/> 
<w:noProof/> 
</w:rPr> 
        **<w:t>1</w:t>** 
</w:r> 
<w:r w:rsidR="009B5500"><w:rPr><w:rFonts w:eastAsiaTheme="minorEastAsia"/> 
<w:b w:val="0"/> 
<w:noProof/> 
<w:color w:val="auto"/> 
<w:lang w:eastAsia="de-DE"/> 
</w:rPr><w:tab/> 
</w:r> 
<w:r w:rsidR="009B5500" w:rsidRPr="009D0220"> 
<w:rPr> 
<w:rStyle w:val="Hyperlink"/> 
<w:noProof/> 
</w:rPr> 
        **<w:t>Management Summary</w:t>** 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
<w:tab/> 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr><w:fldChar w:fldCharType="begin"/> 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
<w:instrText xml:space="preserve"> PAGEREF _Toc474825312 \h </w:instrText> 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
<w:fldChar w:fldCharType="separate"/> 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
       **<w:t>6</w:t>** 
</w:r> 
<w:r w:rsidR="009B5500"> 
<w:rPr> 
<w:noProof/> 
<w:webHidden/> 
</w:rPr> 
<w:fldChar w:fldCharType="end"/> 
</w:r> 
</w:hyperlink> 
</w:p> 

이 인덱스의 첫 번째 항목이다 1. 관리 개요 6

+1

그것은 입력 XML – GGamba

+0

나는 내용을 포함 serveral P 패키지 내용 사이에있다, 그것은이 마지막 페이지 패키지 아래 이해하는 것이 지금은 더 나은 희망의 최소한의 예를 가지고 도움이 될 수 있습니다

+0

나는'w' 접두사 정의를 놓친다 고 생각합니다. xml에서'w = "http : // schemas ......"와 같은 것을 찾을 수 있습니까? – GGamba

답변

0

우리는 사용할 수 있습니다

library(xml2) 
library(magrittr) 

x <- read_xml("path/to/file.xml") 

titles <- xml_find_all(x, 
       "/pkg:package//pkg:part/pkg:xmlData/w:document/w:body/w:p/w:hyperlink/w:r/w:t") %>% 
     xml_text() %>% 
     matrix(ncol = 3, byrow = T) %>% 
     as.data.frame() 

colnames(titles)<- c('numChapter', 'title', 'numPage') 

이 모든 노드 corresp 내부의 텍스트를 retrives 그 xpath에 onding.

예를 들어 xpath에 (내가 가정 한 것) numChapter, 해당 title 및 해당 numPage이 들어 있습니다.


xml의 형식이 잘못되거나 일부 네임 스페이스가 누락되면 오류가 발생합니다.

희망이

+0

Mhh unfortunatelly not really. xmlSApply (x, xmlValue)) xml_df <- data.frame ((part_xml), row.names = 'part_xml <- xmlSApply (topxml [3] [[1] NULL)'은 동일한 출력을가집니다.내 문제는 출력에 구조가 없으므로 텍스트가 장 제목으로 연결되거나 그룹화되지 않는다는 것입니다. –

+0

여기서 xml은 원하는 '구조화 된'결과입니까? 나는 'Inhaltsverzeichnis'가 당신이 관심을 가지고있는 첫 번째 노드 였고, 후에 비슷한 몇몇 노드가 있다고 가정합니다. – GGamba

+0

이 편집되었는지 확인하십시오. 관심있는 내용이 무엇인지 확인하십시오. – GGamba