質問

Nokogiriを使用してXMLファイルを解析していて動作していません。

ノードから3つのレベルの上につながりようとしているときは、タイプの最初のノードからデータを掴みます。私はそれをデバッグし、それがオンになっているノードは私が必要とするデータを取得するための正しいものであるべきですが、それはまだそのタイプの最初のノードを引いています。

上位ノードにない項目はファイルの罰金に出力されていますが、ツリーの移動を開始するとファイルに間違ったデータを書き込んでいます。

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>
.

これは、Nokogiriを使って初めてここで何か悪いことをしているかもしれません。

役に立ちましたか?

解決

問題

問題は、//を使用したXPathの開始です。これは文書内の任意の場所にあるノードを見つけることを意味します。

次の簡素化された例では、//を使用すると(繰り返しの項目のSubitemではなく)同じSIBITEMが返されることがわかります。

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
.

特定のノード内のどこにでも見たい場合は、Pimers-IE .//から始める必要があります。これを簡略化した例に適用すると、予想されるSIBITEM結果が得られることがわかります。

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

溶液

あなたの特定の問題のために、XPathをアイテムの反復に変更して、.を開始時に変更する必要があります。たとえば、行:

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

に変更されます。

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

全体これはあなたに与えるでしょう:

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
.

結果と:

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
.

注:構造がわからない限り、/の代わりに単一の//を使用することをお勧めします。 /は直接子ノードをチェックします。これにより、予期しない結果が得られればデバッグが簡単になります。

他のヒント

これはコードレビューの少しになるだろうが、それはあなたの将来の解析であなたを助けるでしょう。

  1. CSVを作成しようとしている場合は、CSV GEMを使用することをお勧めします。それはあなたが壊れたCSVを作成することはないことを保証します(コードの1つのフィールドにコンマが含まれている瞬間にコードを作成する)。また、変数名を使用してもう少し説明的になることができます。

  2. 他のもので言及されているように、//を使用できる場合は、/を使用しないでください。あなたが本当に欲しいものではないことに加えてパフォーマンスの罰があります。

  3. item.parent.parent.parentの急増は、あなたがあなたの道の木の木をはるかに下落したことを伝えます。正しいレベルにあることを確認するために、XPathの述語([])を利用してください。

  4. また、XPathを利用している限り、XPATHに最初の7以上のものを与えないように指示すると、索引やbreakは必要ありません。あなたのデータを考えると私はあなたがそれをまったく必要にするかどうかわからない。

  5. 例:

    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
    
    .

rootのrootの要素のCWitemexportと子がありません。itemexportdata要素。XMLを次のように変更した場合:

<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>
.

GrammyStext.txtファイルに次のことを取得します。

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
.

あるいは、ファイルを読み取った後に必要な要素を追加することができます。

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
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top