rubyxmlparsingnokogiriwordml

XML Parsing Broken-up Text


I'm looking to parse a word document XML to get the footer information per each paragraph, sentence, or phrase.

This command gets all the text without spaces in between.

pry(main)> doc.header_and_footers_xml[1].text()
=> " PAGE 1FirstGoogle.comSomething privacy Saturday, February 23, 2019"

This command is a little bit better but breaks up the text in weird ways:

pry(main)> doc.header_and_footers_xml[1].search('//text()')
=> [#<Nokogiri::XML::Text:0x3fdf0eb0c3a4 " PAGE ">, #<Nokogiri::XML::Text:0x3fdf10c41b78 "1">, #<Nokogiri::XML::Text:0x3fdf0eaa427c "F">, #<Nokogiri::XML::Text:0x3fdf0ea60bbc "irst">, #<Nokogiri::XML::Text:0x3fdf0e9f9bc4 "Google.com">, #<Nokogiri::XML::Text:0x3fdf0f6b636c "Something privacy">, #<Nokogiri::XML::Text:0x3fdf0b9ded90 " Saturday, February 23, 2019">]

pry(main)> doc.header_and_footers_xml[1].search('//text()')[2]
=> #(Text "F")

pry(main)> doc.header_and_footers_xml[1].search('//text()')[3]
=> #(Text "irst")

I would like to receive iterate through a list/array with elements: 'PAGE', '1', 'First', 'Google.com', 'Something privacy', 'Saturday, February 23, 2019'

Below is the entire XML. Is it possible just to iterate on name = "p" elements?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:sdt>
        <w:sdtPr>
            <w:rPr>
                <w:rStyle w:val="PageNumber" />
            </w:rPr>
            <w:id w:val="-157074914" />
            <w:docPartObj>
            <w:docPartGallery w:val="Page Numbers (Top of Page)" />
            <w:docPartUnique />
            </w:docPartObj>
        </w:sdtPr>
        <w:sdtEndPr>
            <w:rPr>
                <w:rStyle w:val="PageNumber" />
            </w:rPr>
        </w:sdtEndPr>
        <w:sdtContent>
            <w:p w:rsidR="00140C14" w:rsidRDefault="00140C14" w:rsidP="00AD16D8">
                <w:pPr>
                    <w:pStyle w:val="Header" />
                    <w:framePr w:wrap="none" w:vAnchor="text" w:hAnchor="margin" w:xAlign="right" w:y="1" />
                    <w:rPr>
                        <w:rStyle w:val="PageNumber" />
                    </w:rPr>
                </w:pPr>
                <w:r>
                    <w:rPr>
                        <w:rStyle w:val="PageNumber" />
                    </w:rPr>
                    <w:fldChar w:fldCharType="begin" />
                </w:r>
                <w:r>
                    <w:rPr>
                        <w:rStyle w:val="PageNumber" />
                    </w:rPr>
                    <w:instrText xml:space="preserve"> PAGE </w:instrText>
                </w:r>
                <w:r>
                    <w:rPr>
                        <w:rStyle w:val="PageNumber" />
                    </w:rPr>
                    <w:fldChar w:fldCharType="separate" />
                </w:r>
                <w:r>
                    <w:rPr>
                            <w:rStyle w:val="PageNumber" />
                        <w:noProof />
                    </w:rPr>
                    <w:t>1</w:t>
                </w:r>
                <w:r>
                    <w:rPr>
                        <w:rStyle w:val="PageNumber" />
                    </w:rPr>
                    <w:fldChar w:fldCharType="end" />
                </w:r>
            </w:p>
        </w:sdtContent>
    </w:sdt>
    <w:p w:rsidR="002132D5" w:rsidRDefault="00140C14" w:rsidP="00140C14">
        <w:pPr>
            <w:pStyle w:val="Header" />
            <w:ind w:right="360" />
        </w:pPr>
        <w:r>
            <w:t>F</w:t>
        </w:r>
        <w:r w:rsidR="002132D5">
            <w:t>irst</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="00140C14" w:rsidRDefault="00140C14" w:rsidP="00140C14">
        <w:pPr>
            <w:pStyle w:val="Header" />
            <w:ind w:right="360" />
        </w:pPr>
        <w:r>
        <w:t>Google.com</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="00140C14" w:rsidRDefault="00140C14" w:rsidP="00140C14">
        <w:pPr>
            <w:pStyle w:val="Header" />
            <w:ind w:right="360" />
        </w:pPr>
        <w:r>
            <w:t>Something privacy</w:t>
        </w:r>
        <w:r w:rsidR="00710468">
            <w:t xml:space="preserve"> Saturday, February 23, 2019</w:t>
        </w:r>
        <w:bookmarkStart w:id="0" w:name="_GoBack" />
        <w:bookmarkEnd w:id="0" />
    </w:p>
</w:hdr>

Here is the word document:

actual layout in Word document


Solution

  • The principle is like this:

    1. Define namespace URIs, so we can search for <w:p> elements properly with XPath. In this case only the w: namespace is relevant.
    2. Iterate the <w:p> nodes
    3. Join the non-empty text nodes in each of them

    This is probably horribly non-idiomatic Ruby, but it should get you started:

    require 'nokogiri'
    
    header_and_footers_xml = Nokogiri::XML(open("footer.xml"))  
    
    namespaces = {
      "w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    }
    
    paras = header_and_footers_xml.search('//w:p', namespaces)
    paras.each do |p|
      nodes = p.xpath('.//text()[normalize-space()]')
      texts = nodes.map { |n| n.text }
      puts(texts.join)
    end
    

    This prints (tested on Ruby 2.5):

     PAGE 1
    First
    Google.com
    Something privacy Saturday, February 23, 2019
    

    The XPath expression .//text()[normalize-space()] collects all text nodes (text()) descendant of the current node (.) and filters out the blank ones by calling normalize-space() on each of them, which trims off the whitespace - only those nodes are returned, where a non-empty string remains after trimming.