2010-09-24 29 views
7

Phần tử PythonTree có vẻ không sử dụng được với các không gian tên. Lựa chọn thay thế của tôi là gì? BeautifulSoup cũng khá là rác với không gian tên. Tôi không muốn xóa chúng ra.Trình phân tích cú pháp XML python tốt để làm việc với các tài liệu không gian tên lớn

Ví dụ về cách một thư viện python cụ thể nhận các phần tử không gian tên và bộ sưu tập của chúng là tất cả +1.

Chỉnh sửa: Bạn có thể cung cấp mã để xử lý trường hợp sử dụng thực tế này bằng thư viện bạn chọn không?

Làm thế nào bạn sẽ đi về nhận chuỗi 'Line Break', '2,6' và một danh sách [ 'PYTHON', 'XML', 'XML namespace']

<?xml version="1.0" encoding="UTF-8"?> 
<zs:searchRetrieveResponse 
    xmlns="http://unilexicon.com/vocabularies/" 
    xmlns:zs="http://www.loc.gov/zing/srw/" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:lom="http://ltsc.ieee.org/xsd/LOM"> 
    <zs:records> 
     <zs:record> 
      <zs:recordData> 
       <srw_dc:dc xmlns:srw_dc="info:srw/schema/1/dc-schema"> 
        <name>Line Break</name> 
        <dc:title>Processing XML namespaces using Python</dc:title> 
        <dc:description>How to get contents string from an element, 
         how to get a collection in a list...</dc:description> 
        <lom:metaMetadata> 
         <lom:identifier> 
          <lom:catalog>Python</lom:catalog> 
          <lom:entry>2.6</lom:entry> 
         </lom:identifier> 
        </lom:metaMetadata> 
        <lom:classification> 
         <lom:taxonPath> 
          <lom:taxon> 
           <lom:id>PYTHON</lom:id> 
          </lom:taxon> 
         </lom:taxonPath> 
        </lom:classification> 
        <lom:classification> 
         <lom:taxonPath> 
          <lom:taxon> 
           <lom:id>XML</lom:id> 
          </lom:taxon> 
         </lom:taxonPath> 
        </lom:classification> 
        <lom:classification> 
         <lom:taxonPath> 
          <lom:taxon> 
           <lom:id>XML-NAMESPACES</lom:id> 
          </lom:taxon> 
         </lom:taxonPath> 
        </lom:classification> 
       </srw_dc:dc> 
      </zs:recordData> 
     </zs:record> 
     <!-- ... more records ... --> 
    </zs:records> 
</zs:searchRetrieveResponse> 
+1

Tôi yêu thiên nhiên meta của MWe của bạn. –

+0

Sử dụng các từ khóa có liên quan trong mã ví dụ có nghĩa là nhiều người dùng hơn có thể tìm thấy câu hỏi và câu trả lời. –

Trả lời

12

lxml là nhận biết không gian tên.

>>> from lxml import etree 
>>> et = etree.XML("""<root xmlns="foo" xmlns:stuff="bar"><bar><stuff:baz /></bar></root>""") 
>>> etree.tostring(et, encoding=str) # encoding=str only needed in Python 3, to avoid getting bytes 
'<root xmlns="foo" xmlns:stuff="bar"><bar><stuff:baz/></bar></root>' 
>>> et.xpath("f:bar", namespaces={"b":"bar", "f": "foo"}) 
[<Element {foo}bar at ...>] 

Chỉnh sửa: Trên ví dụ của bạn:

from lxml import etree 

# remove the b prefix in Python 2 
# needed in python 3 because 
# "Unicode strings with encoding declaration are not supported." 
et = etree.XML(b"""...""") 

ns = { 
    'lom': 'http://ltsc.ieee.org/xsd/LOM', 
    'zs': 'http://www.loc.gov/zing/srw/', 
    'dc': 'http://purl.org/dc/elements/1.1/', 
    'voc': 'http://www.schooletc.co.uk/vocabularies/', 
    'srw_dc': 'info:srw/schema/1/dc-schema' 
} 

# according to docs, .xpath returns always lists when querying for elements 
# .find returns one element, but only supports a subset of XPath 
record = et.xpath("zs:records/zs:record", namespaces=ns)[0] 
# in this example, we know there's only one record 
# but else, you should apply the following to all elements the above returns 

name = record.xpath("//voc:name", namespaces=ns)[0].text 
print("name:", name) 

lom_entry = record.xpath("zs:recordData/srw_dc:dc/" 
         "lom:metaMetadata/lom:identifier/" 
         "lom:entry", 
         namespaces=ns)[0].text 

print('lom_entry:', lom_entry) 

lom_ids = [id.text for id in 
      record.xpath("zs:recordData/srw_dc:dc/" 
         "lom:classification/lom:taxonPath/" 
         "lom:taxon/lom:id", 
         namespaces=ns)] 

print("lom_ids:", lom_ids) 

Output:

name: Frank Malina 
lom_entry: 2.6 
lom_ids: ['PYTHON', 'XML', 'XML-NAMESPACES'] 
+2

+1 lxml là công cụ/gói python duy nhất bạn cần cho các tác vụ liên quan xml/xslt/xpath – snapshoe

+0

Chỉnh sửa: Bạn sẽ viết mã xung quanh ví dụ được cung cấp như thế nào? Việc thiếu công thức nấu ăn trên web cho loại công việc lxml này là kinh khủng. Tại thời điểm này, tôi đã tiến hành bằng cách tước các không gian tên và đi ngang qua với BeautifulSoup. Điều này là tối ưu trên một số cấp độ. –

+0

@Frank Malina: XPath không phải là lxml cụ thể, có một số tài nguyên có thể sử dụng trên XPath trên web. Nhưng tôi sẽ đâm vào nó ... – delnan

0

libxml (http://xmlsoft.org/) nhất, lib nhanh hơn cho xml phân tích cú pháp. Có triển khai cho python.

+4

lxml từ mã kết thúc tốt đẹp và sử dụng libxml – snapshoe

Các vấn đề liên quan