2017-07-19 1 views
0

Ich habe [Content_Types].xml Datei:Kann keine Knoten finden Nokogiri mit

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> 
    <Default Extension="xml" ContentType="application/xml"/> 
    <Default Extension="jpeg" ContentType="image/jpeg"/> 
    <Default Extension="png" ContentType="image/png"/> 
    <Default Extension="jpg" ContentType="image/jpeg"/> 
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> 
    <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> 
    <Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml"/> 
    <Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/> 
    <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/> 
    <Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/> 
    <Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/> 
    <Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/> 
    <Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/> 
    <Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> 
    <Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> 
    <Override PartName="/word/header2.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> 
    <Override PartName="/word/footer2.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> 
    <Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/> 
    <Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/> 
    <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> 
    <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> 
</Types> 

ich es mit Nokogiri geladen haben:

doc = File.open("[Content_Types].xml") { |f| Nokogiri::XML(f) } 

möchte ich finden den <Default Extension="png" ContentType="image/png"/> Knoten aber ich kann nicht nichts finden:

irb(main):048:0> doc.xpath('//Default') 
=> [] 
irb(main):049:0> doc.xpath('//Override') 
=> [] 
irb(main):050:0> doc.xpath('//Types') 
=> [] 
irb(main):051:0> doc.xpath('Types') 
=> [] 

Warum?

ist das XML korrekt geladen:

irb(main):003:0> doc 
=> 
#<Nokogiri::XML::Document:0x3fcddd413ad0 name="document" children=[#<Nokogiri::XML::Element:0x3fcddd41347c name="Types" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> children=[#<Nokogiri::XML::Text:0x3fcddd412f18 "\n\n ">, #<Nokogiri::XML::Element:0x3fcddd412e14 name="Default" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd412d9c name="Extension" value="xml">, #<Nokogiri::XML::Attr:0x3fcddd412d88 name="ContentType" value="application/xml">]>, #<Nokogiri::XML::Text:0x3fcddd4126bc "\n ">, #<Nokogiri::XML::Element:0x3fcddd413558 name="Default" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd40ffac name="Extension" value="rels">, #<Nokogiri::XML::Attr:0x3fcddd40ff84 name="ContentType" value="application/vnd.openxmlformats-package.relationships+xml">]>, #<Nokogiri::XML::Text:0x3fcddd40ef08 "\n ">, #<Nokogiri::XML::Element:0x3fcddd40eddc name="Default" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd40ecec name="Extension" value="jpeg">, #<Nokogiri::XML::Attr:0x3fcddd40ecc4 name="ContentType" value="image/jpeg">]>, #<Nokogiri::XML::Text:0x3fcddd40bca4 "\n ">, #<Nokogiri::XML::Element:0x3fcddd40bb78 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd40bac4 name="PartName" value="/word/document.xml">, #<Nokogiri::XML::Attr:0x3fcddd40ba88 name="ContentType" value="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">]>, #<Nokogiri::XML::Text:0x3fcddd40a8cc "\n ">, #<Nokogiri::XML::Element:0x3fcddd40a7f0 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd40a778 name="PartName" value="/word/styles.xml">, #<Nokogiri::XML::Attr:0x3fcddd40a764 name="ContentType" value="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">]>, #<Nokogiri::XML::Text:0x3fcddd099d00 "\n ">, #<Nokogiri::XML::Element:0x3fcddd099bc0 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd099b34 name="PartName" value="/word/settings.xml">, #<Nokogiri::XML::Attr:0x3fcddd099b20 name="ContentType" value="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml">]>, #<Nokogiri::XML::Text:0x3fcddd098e8c "\n ">, #<Nokogiri::XML::Element:0x3fcddd098d60 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd098cc0 name="PartName" value="/word/webSettings.xml">, #<Nokogiri::XML::Attr:0x3fcddd098cac name="ContentType" value="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml">]>, #<Nokogiri::XML::Text:0x3fcddd08ded8 "\n ">, #<Nokogiri::XML::Element:0x3fcddd08dd98 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd08dce4 name="PartName" value="/word/fontTable.xml">, #<Nokogiri::XML::Attr:0x3fcddd08dca8 name="ContentType" value="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml">]>, #<Nokogiri::XML::Text:0x3fcddd08cdf8 "\n ">, #<Nokogiri::XML::Element:0x3fcddd08cd1c name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd08cc54 name="PartName" value="/word/theme/theme1.xml">, #<Nokogiri::XML::Attr:0x3fcddd08cc40 name="ContentType" value="application/vnd.openxmlformats-officedocument.theme+xml">]>, #<Nokogiri::XML::Text:0x3fcddd08c0d8 "\n ">, #<Nokogiri::XML::Element:0x3fcddd08c010 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd089fa4 name="PartName" value="/docProps/core.xml">, #<Nokogiri::XML::Attr:0x3fcddd089f90 name="ContentType" value="application/vnd.openxmlformats-package.core-properties+xml">]>, #<Nokogiri::XML::Text:0x3fcddd089388 "\n ">, #<Nokogiri::XML::Element:0x3fcddd089248 name="Override" namespace=#<Nokogiri::XML::Namespace:0x3fcddd4133b4 href="http://schemas.openxmlformats.org/package/2006/content-types"> attributes=[#<Nokogiri::XML::Attr:0x3fcddd089144 name="PartName" value="/docProps/app.xml">, #<Nokogiri::XML::Attr:0x3fcddd089130 name="ContentType" value="application/vnd.openxmlformats-officedocument.extended-properties+xml">]>]>]> 
+0

Sie geschrieben "Default" als "Deafult" in Ihrer Konsole Beispiel –

+0

@SimpleLime Das war ein Tippfehler in Frage - es ist jetzt korrigiert. Danke, dass du es gesehen hast. ;) – pmichna

Antwort

2

Auf der "Searching a XML/HTML document" Seite auf der Website Nokogiri, ein ATOM Beispiel gibt es

das diesen Atom-Feed zum Beispiel nehmen lassen:

<?xml version="1.0" encoding="utf-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <title>Example Feed</title> 
    <link href="http://example.org/"/> 
    <updated>2003-12-13T18:30:02Z</updated> 
    <author> 
    <name>John Doe</name> 
    </author> 
    <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> 
    <entry> 
    <title>Atom-Powered Robots Run Amok</title> 
    <link href="http://example.org/2003/12/13/atom03"/> 
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> 
    <updated>2003-12-13T18:30:02Z</updated> 
    <summary>Some text.</summary> 
    </entry> 
</feed> 

Wenn wir uns an die Konvention halten, können wir alle Titel-Tags wie diese

greifen
@doc.xpath('//xmlns:title') # => ["<title>Example Feed</title>", "<title>Atom-Powered Robots Run Amok</title>"] 

Da Ihr Beispiel Eingangs

<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> 

hat Es sollte

puts doc.xpath("//xmlns:Default[@Extension='png']") 
# <Default Extension="png" ContentType="image/png"/> 

Alternativ zu tun arbeiten, Sie css statt

puts doc.css("Types Default[Extension='png']") 
# <Default Extension="png" ContentType="image/png"/> 

Es gibt auch verwenden können, ein Abschnitt auf der Seite über not dealing with namespaces Wenn Sie interessiert sind