2016-06-01 4 views
0

다음은 FODT 파일의 추출에서 코드 조각입니다 속성 :추출 데이터

<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2" office:mimetype="application/vnd.oasis.opendocument.text"> 

내가 각 네임 스페이스의 내용을 구분합니다. 예를 들어 xmlns : office = "urn : oasis : names : tc : opendocument : xmlns : office : 1.0", xmlns : style = "urn : oasis : names : tc : opendocument : xmlns : style : 1.0" 등 을 포함하여 네임 스페이스 이름 자체.

어떻게 lxml을 사용합니까?

+0

는 "각 네임 스페이스의 내용을 분리"무엇을 의미합니까? 선언 된 네임 스페이스를 모두 나열 하시겠습니까? – mzjn

+1

@mzjn 해당 용어를 사용하지 않으면 죄송합니다. 위의 코드에서 나는이 종류의 목록을 가지고 싶습니다. - '[xmlns : office = "urn : oasis : names : tc : opendocument : xmlns : office : 1.0", xmlns : text = "urn : oasis : 이름 : tc : opendocument : xmlns : text : 1.0 ", ...]' –

답변

2

루트 요소의 nsmap 속성은 선언 된 모든 네임 스페이스가있는 사전을 보유합니다. 예 :

from lxml import etree 

XML = "your XML document here..." 

root = etree.fromstring(XML) 
for ns in sorted(root.nsmap.items()): 
    print ns 

출력 :

('calcext', 'urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0') 
('chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0') 
('config', 'urn:oasis:names:tc:opendocument:xmlns:config:1.0') 
('css3t', 'http://www.w3.org/TR/css3-text/') 
('dc', 'http://purl.org/dc/elements/1.1/') 
('dom', 'http://www.w3.org/2001/xml-events') 
('dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0') 
('draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0') 
('drawooo', 'http://openoffice.org/2010/draw') 
('field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0') 
('fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0') 
('form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0') 
('formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0') 
('grddl', 'http://www.w3.org/2003/g/data-view#') 
('loext', 'urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0') 
('math', 'http://www.w3.org/1998/Math/MathML') 
('meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0') 
('number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0') 
('of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2') 
('office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0') 
('officeooo', 'http://openoffice.org/2009/office') 
('ooo', 'http://openoffice.org/2004/office') 
('oooc', 'http://openoffice.org/2004/calc') 
('ooow', 'http://openoffice.org/2004/writer') 
('rpt', 'http://openoffice.org/2005/report') 
('script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0') 
('style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0') 
('svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0') 
('table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0') 
('tableooo', 'http://openoffice.org/2009/table') 
('text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0') 
('xforms', 'http://www.w3.org/2002/xforms') 
('xhtml', 'http://www.w3.org/1999/xhtml') 
('xlink', 'http://www.w3.org/1999/xlink') 
('xsd', 'http://www.w3.org/2001/XMLSchema') 
('xsi', 'http://www.w3.org/2001/XMLSchema-instance') 
+0

정말 고마워요! 이것은 멍청한 질문 이었지만 앞으로의 작업을 간소화하는 데 큰 도움이됩니다. –

+0

다른 질문이 있습니다. 내 질문에 코드 스 니펫을 살펴 본다면, 이것은 'office : version = "1.2"office : mimetype = "application/vnd.oasis.opendocument.text"' 끝에 표시됩니다. 어떻게 추출합니까? 그들은 네임 스페이스가 아니기 때문에 같은 방식으로 추출 할 수 없습니다. –

+1

'office : version = "1.2"'이고'office : mimetype = "application/vnd.oasis.opendocument.text"는 정규 속성 (네임 스페이스 선언이 아님)이다. 그것들은 루트 요소의'attrib' 속성에 저장됩니다. 'items()'메소드를 통해서도 가능합니다. http://lxml.de/api/lxml.etree._Element-class.html을 참조하십시오. – mzjn