Question

I am using Nokogiri to parse an XML file and it is not working.

When I am trying to grab from a node three levels up it is grabbing the data from the first node of the type. I have debugged it and the node it is on should be the right one to get the data I need but it is still pulling the data form the first node of that type.

The items that are not in the higher level nodes are outputting to the file fine but when I start moving up the tree it is writing wrong data to the file.

require 'nokogiri'

f = File.new("grammystext.txt", "w+")
x = File.open("items.xml", "r")
doc = Nokogiri::XML(x)
x.close

doc.xpath('//CWItemExport//ItemExportData//CWItem//ProductID//ItemColor//ItemSize').each_with_index do |item, i|
  f << item.parent.parent.parent.at_xpath('//CWVendor//VendorCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//CWVendor//VendorName').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemStyle').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDescription').content + ", "
  f << item.parent.parent.parent.at_xpath('//TaxID//TaxIDCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDepartment//ItemDeptCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDepartment//ItemDeptName').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemClass//ItemClassCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemClass//ItemClassName').content + ", "

  f << item.attr("MainSize") + ", "
  f << item.at_xpath('Sku').content + ", "
  f << item.at_xpath('//ReplacementCost').content + ", "
  f << item.at_xpath('//CurrentRetail').content + "\n"

  puts item.parent.parent.parent if i == 6

  break if i == 7
end

f.close

XML:

<CWItem action="New">
  <CWVendor>
   <VendorCode>5TH</VendorCode>
   <VendorName>5TH SUN</VendorName>
    <VendorAddress />
    <VendorAddress2 />
    <VendorCity />
    <VendorZip />
    <VendorPhone />
  </CWVendor>
 <ItemStyle>AMM024-B105</ItemStyle>
 <ItemDescription>CALVERY</ItemDescription>
  <ItemBoolPLU>N</ItemBoolPLU>
  <TaxID>
    <TaxIDCode TaxStore="1" TaxIDType="Normal">0</TaxIDCode>
    <ComponentTax TxID="0" TxType="Normal" TxStartAmt="0.00" TxEndAmt="100000000.00" TxGlPayAcct=" ">0.000</ComponentTax>
  </TaxID>
  <ItemDepartment>
   <ItemDeptCode>APPAR</ItemDeptCode>
   <ItemDeptName>APPAR</ItemDeptName>
  </ItemDepartment>
  <ItemClass>
    <ItemClassCode>TEE</ItemClassCode>
   <ItemClassName>TEE-SHIRTS</ItemClassName>
  </ItemClass>
  <ItemSizeRun SizeRunCode="RUN" SizeRunName="">
    <SizeDef SizeLabel="">
      <Size SizeLabel="XS" Sequence="0">XS</Size>
      <Size SizeLabel="S" Sequence="1">S</Size>
      <Size SizeLabel="M" Sequence="2">M</Size>
      <Size SizeLabel="L" Sequence="3">L</Size>
      <Size SizeLabel="XL" Sequence="4">XL</Size>
      <Size SizeLabel="XXL" Sequence="5">XXL</Size>
    </SizeDef>
  </ItemSizeRun>
<ProductID PID="">
  <ItemColor ColorCode="N/A" ColorName="">
   <ItemSize MainSize="L">
      <Sku>400100018477</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="M">
      <Sku>400100018460</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="S">
      <Sku>400100018453</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XL">
      <Sku>400100018484</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XS">
      <Sku>400100031704</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>0.00</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XXL">
      <Sku>400100035801</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>0.00</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
  </ItemColor>
</ProductID>
</CWItem>
<CWItem action="New">
  <CWVendor>
    <VendorCode>5TH</VendorCode>
    <VendorName>5TH SUN</VendorName>
    <VendorAddress />
    <VendorAddress2 />
    <VendorCity />
    <VendorZip />
    <VendorPhone />
  </CWVendor>
  <ItemStyle>AMM025-B105</ItemStyle>
  <ItemDescription>WINGMAN</ItemDescription>
  <ItemBoolPLU>N</ItemBoolPLU>
  <TaxID>
    <TaxIDCode TaxStore="1" TaxIDType="Normal">0</TaxIDCode>
    <ComponentTax TxID="0" TxType="Normal" TxStartAmt="0.00" TxEndAmt="100000000.00" TxGlPayAcct=" ">0.000</ComponentTax>
  </TaxID>
  <ItemDepartment>
    <ItemDeptCode>APPAR</ItemDeptCode>
    <ItemDeptName>APPAR</ItemDeptName>
  </ItemDepartment>
  <ItemClass>
    <ItemClassCode>TEE</ItemClassCode>
    <ItemClassName>TEE-SHIRTS</ItemClassName>
  </ItemClass>
  <ItemSizeRun SizeRunCode="RUN" SizeRunName="">
    <SizeDef SizeLabel="">
      <Size SizeLabel="XS" Sequence="0">XS</Size>
      <Size SizeLabel="S" Sequence="1">S</Size>
      <Size SizeLabel="M" Sequence="2">M</Size>
      <Size SizeLabel="L" Sequence="3">L</Size>
      <Size SizeLabel="XL" Sequence="4">XL</Size>
      <Size SizeLabel="XXL" Sequence="5">XXL</Size>
    </SizeDef>
  </ItemSizeRun>
<ProductID PID="">
  <ItemColor ColorCode="N/A" ColorName="">
    <ItemSize MainSize="L">
      <Sku>400100018514</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="M">
      <Sku>400100018507</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="S">
      <Sku>400100018491</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XL">
      <Sku>400100018521</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>44.80</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XS">
      <Sku>400100031711</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>0.00</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
    <ItemSize MainSize="XXL">
      <Sku>400100035818</Sku>
      <Pricing Currency="USD">
        <ReplacementCost>44.80</ReplacementCost>
        <AverageCost>0.00</AverageCost>
        <LandedCost>0.00</LandedCost>
        <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
      </Pricing>
    </ItemSize>
  </ItemColor>
</ProductID>
</CWItem>

This is my first time using Nokogiri so I may be doing something wrong here.

Was it helpful?

Solution

Problem

The problem is the starting of the xpaths with //. This says to locating the node anywhere in the document.

In the following simplified example, you can see that using // results in the same subitem being returned (rather than the subitem of the item of the iteration).

require 'nokogiri'

xml = %Q{
<root>
  <item>
    <subitem>1</subitem>
  </item>
  <item>
    <subitem>2</subitem>
  </item>  
</root>
}

doc = Nokogiri::XML(xml)
doc.xpath('//root//item').each_with_index do |item, i|
    puts item.at_xpath('//subitem').content
end
#=> 1
#=> 1

If you want to look anywhere within a specific node, you need to start with a period - ie .//. Applying this to the simplified example, you can see that we get the expected subitem results:

doc = Nokogiri::XML(xml)
doc.xpath('//root//item').each_with_index do |item, i|
    puts item.at_xpath('.//subitem').content
end
#=> 1
#=> 2

Solution

For your specific problem, you should change the xpaths in the iteration of the items to include the . at the start. For example the line:

f << item.parent.parent.parent.at_xpath('//CWVendor//VendorCode').content + ", "

Would be changed to:

    f << item.parent.parent.parent.at_xpath('.//CWVendor//VendorCode').content + ", "

Overall this would give you:

doc.xpath('.//CWItemExport//ItemExportData//CWItem//ProductID//ItemColor//ItemSize').each_with_index do |item, i|
  f << item.parent.parent.parent.at_xpath('.//CWVendor//VendorCode').content + ", "
  f << item.parent.parent.parent.at_xpath('.//CWVendor//VendorName').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemStyle').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemDescription').content + ", "
  f << item.parent.parent.parent.at_xpath('.//TaxID//TaxIDCode').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemDepartment//ItemDeptCode').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemDepartment//ItemDeptName').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemClass//ItemClassCode').content + ", "
  f << item.parent.parent.parent.at_xpath('.//ItemClass//ItemClassName').content + ", "

  f << item.attr("MainSize") + ", "
  f << item.at_xpath('Sku').content + ", "
  f << item.at_xpath('.//ReplacementCost').content + ", "
  f << item.at_xpath('.//CurrentRetail').content + "\n"

  puts item.parent.parent.parent if i == 6

  break if i == 7
end

With the results:

5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, L, 400100018477, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, M, 400100018460, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, S, 400100018453, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XL, 400100018484, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XS, 400100031704, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XXL, 400100035801, 44.80, 199.00
5TH, 5TH SUN, AMM025-B105, WINGMAN, 0, APPAR, APPAR, TEE, TEE-SHIRTS, L, 400100018514, 44.80, 199.00

Note: I would also recommend using single / instead of // unless the structure is not known. The / checks for direct children nodes, which makes it easier to debug if you get unexpected results.

OTHER TIPS

This is going to be a bit of a code review, but hopefully it will help you with your future parsing.

  1. If you are going to create CSV, I highly recommend you use the CSV gem. It guarantees you won't create broken CSV (which your code would create the moment one of the fields contains a comma). Also, it allows you to be a little more descriptive using variable names.

  2. As has been mentioned by others, don't use // if you can use /. There's a performance penalty in addition to not being what you really want in some cases.

  3. The proliferation of item.parent.parent.parent tells me you went too far down the tree in your path. Take advantage of XPath's predicates (in []) to ensure you are at the correct level.

  4. Also, as long as we're taking advantage of XPath, you won't need an index or a break when you tell XPath not to give you more than the first 7. I didn't implement this because given your data I'm not sure if you need it at all.

Example:

CSV.open("grammystext.csv", "wb") do |csv|
  items = doc.xpath('/CWItemExport/ItemExportData/CWItem[ProductID/ItemColor/ItemSize]')       items.each do |item|
    vendor_code = item.at_xpath('CWVendor/VendorCode').text
    vendor_name = item.at_xpath('CWVendor/VendorName').text
    item_style  = item.at_xpath('ItemStyle').text
    ...
    main_size = item.at_xpath('ProductID/ItemColor/ItemSize')['MainSize']
    sku       = item.at_xpath('ProductID/ItemColor/ItemSize/Sku').text

    csv << [vendor_code, vendor_name, item_style, ... , main_size, sku]
  end
end

You have no root element CWItemExport and child of root: ItemExportData element. If you change your XML to:

<CWItemExport>
    <ItemExportData>


        <CWItem action="New">
            <CWVendor>
                <VendorCode>5TH</VendorCode>

                <VendorName>5TH SUN</VendorName>
                <VendorAddress />
                <VendorAddress2 />
                <VendorCity />
                <VendorZip />
                <VendorPhone />
            </CWVendor>
            <ItemStyle>AMM024-B105</ItemStyle>
            <ItemDescription>CALVERY</ItemDescription>
            <ItemBoolPLU>N</ItemBoolPLU>
            <TaxID>
                <TaxIDCode TaxStore="1" TaxIDType="Normal">0</TaxIDCode>
                <ComponentTax TxID="0" TxType="Normal" TxStartAmt="0.00" TxEndAmt="100000000.00" TxGlPayAcct="
                    ">0.000</ComponentTax>
            </TaxID>
            <ItemDepartment>
                <ItemDeptCode>APPAR</ItemDeptCode>
                <ItemDeptName>APPAR</ItemDeptName>
            </ItemDepartment>
            <ItemClass>
                <ItemClassCode>TEE</ItemClassCode>
                <ItemClassName>TEE-SHIRTS</ItemClassName>
            </ItemClass>
            <ItemSizeRun SizeRunCode="RUN" SizeRunName="">
                <SizeDef SizeLabel="">
                    <Size SizeLabel="XS" Sequence="0">XS</Size>
                    <Size SizeLabel="S" Sequence="1">S</Size>
                    <Size SizeLabel="M" Sequence="2">M</Size>
                    <Size SizeLabel="L" Sequence="3">L</Size>
                    <Size SizeLabel="XL" Sequence="4">XL</Size>
                    <Size SizeLabel="XXL" Sequence="5">XXL</Size>
                </SizeDef>
            </ItemSizeRun>
            <ProductID PID="">
                <ItemColor ColorCode="N/A" ColorName="">
                    <ItemSize MainSize="L">
                        <Sku>400100018477</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="M">
                        <Sku>400100018460</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="S">
                        <Sku>400100018453</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XL">
                        <Sku>400100018484</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XS">
                        <Sku>400100031704</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>0.00</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XXL">
                        <Sku>400100035801</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>0.00</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                </ItemColor>
            </ProductID>
        </CWItem>
        <CWItem action="New">
            <CWVendor>
                <VendorCode>5TH</VendorCode>
                <VendorName>5TH SUN</VendorName>
                <VendorAddress />
                <VendorAddress2 />
                <VendorCity />
                <VendorZip />
                <VendorPhone />
            </CWVendor>
            <ItemStyle>AMM025-B105</ItemStyle>
            <ItemDescription>WINGMAN</ItemDescription>
            <ItemBoolPLU>N</ItemBoolPLU>
            <TaxID>
                <TaxIDCode TaxStore="1" TaxIDType="Normal">0</TaxIDCode>
                <ComponentTax TxID="0" TxType="Normal" TxStartAmt="0.00" TxEndAmt="100000000.00" TxGlPayAcct="
                    ">0.000</ComponentTax>
            </TaxID>
            <ItemDepartment>
                <ItemDeptCode>APPAR</ItemDeptCode>
                <ItemDeptName>APPAR</ItemDeptName>
            </ItemDepartment>
            <ItemClass>
                <ItemClassCode>TEE</ItemClassCode>
                <ItemClassName>TEE-SHIRTS</ItemClassName>
            </ItemClass>
            <ItemSizeRun SizeRunCode="RUN" SizeRunName="">
                <SizeDef SizeLabel="">
                    <Size SizeLabel="XS" Sequence="0">XS</Size>
                    <Size SizeLabel="S" Sequence="1">S</Size>
                    <Size SizeLabel="M" Sequence="2">M</Size>
                    <Size SizeLabel="L" Sequence="3">L</Size>
                    <Size SizeLabel="XL" Sequence="4">XL</Size>
                    <Size SizeLabel="XXL" Sequence="5">XXL</Size>
                </SizeDef>
            </ItemSizeRun>
            <ProductID PID="">
                <ItemColor ColorCode="N/A" ColorName="">
                    <ItemSize MainSize="L">
                        <Sku>400100018514</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="M">
                        <Sku>400100018507</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="S">
                        <Sku>400100018491</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XL">
                        <Sku>400100018521</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>44.80</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XS">
                        <Sku>400100031711</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>0.00</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                    <ItemSize MainSize="XXL">
                        <Sku>400100035818</Sku>
                        <Pricing Currency="USD">
                            <ReplacementCost>44.80</ReplacementCost>
                            <AverageCost>0.00</AverageCost>
                            <LandedCost>0.00</LandedCost>
                            <CurrentRetail MarkDowns=" ">199.00</CurrentRetail>
                        </Pricing>
                    </ItemSize>
                </ItemColor>
            </ProductID>
        </CWItem>
    </ItemExportData>
</CWItemExport>

You get the following in your grammystext.txt file:

5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, L, 400100018477, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, M, 400100018460, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, S, 400100018453, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XL, 400100018484, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XS, 400100031704, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, XXL, 400100035801, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, L, 400100018514, 44.80, 199.00
5TH, 5TH SUN, AMM024-B105, CALVERY, 0, APPAR, APPAR, TEE, TEE-SHIRTS, M, 400100018507, 44.80, 199.00

Alternatively, you could add the required elements after reading the file:

require 'nokogiri'

f = File.new("grammystext.txt", "w+")
x = File.open("items.xml", "r")
xml = "<CWItemExport><ItemExportData>#{x.read}</CWItemExport></ItemExportData>"
doc = Nokogiri::XML(xml)
x.close

doc.xpath('//CWItemExport//ItemExportData//CWItem//ProductID//ItemColor//ItemSize').each_with_index do |item, i|
  f << item.parent.parent.parent.at_xpath('//CWVendor//VendorCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//CWVendor//VendorName').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemStyle').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDescription').content + ", "
  f << item.parent.parent.parent.at_xpath('//TaxID//TaxIDCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDepartment//ItemDeptCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemDepartment//ItemDeptName').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemClass//ItemClassCode').content + ", "
  f << item.parent.parent.parent.at_xpath('//ItemClass//ItemClassName').content + ", "

  f << item.attr("MainSize") + ", "
  f << item.at_xpath('Sku').content + ", "
  f << item.at_xpath('//ReplacementCost').content + ", "
  f << item.at_xpath('//CurrentRetail').content + "\n"

  puts item.parent.parent.parent if i == 6

  break if i == 7
end

f.close
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top