Pregunta

Estoy usando Nokogiri para analizar un archivo XML y no está funcionando.

Cuando estoy tratando de apoderarse de un nodo de tres niveles es el acaparamiento de los datos desde el primer nodo del tipo.He depurado y el nodo está en debe ser el adecuado para obtener los datos que necesito, pero aún es tirar el formulario de datos el primer nodo de ese tipo.

Los elementos que no están en el más alto nivel de nodos de salida para el archivo bien pero cuando voy a empezar a mover el árbol es una escritura incorrecta de datos para el archivo.

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>

Esta es mi primera vez usando Nokogiri lo que puede que esté haciendo algo mal aquí.

¿Fue útil?

Solución

Problema

El problema es el punto de partida de la xpath con //.Esto lo dice para localizar el nodo en cualquier lugar del documento.

En el siguiente ejemplo simplificado, se puede ver que el uso de // resultados en el mismo punto de ser devuelto (en lugar de el punto de el punto de la iteración).

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

Si desea buscar en cualquier parte dentro de un nodo específico, usted necesita comenzar con un periodo - es decir, .//.Aplicando esto al ejemplo simplificado, se puede ver que tenemos el esperado punto los resultados:

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

Solución

Para su problema específico, usted debe cambiar el xpath en la iteración de los elementos a incluir la . en el inicio.Por ejemplo, la línea:

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

Sería cambiado a:

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

En general, esto le dará a usted:

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

Con los resultados:

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

Nota:También recomiendo el uso de una sola / en lugar de // a menos que la estructura no se conoce.El / comprueba directa de los nodos hijos, que hace que sea más fácil de depurar si obtiene resultados inesperados.

Otros consejos

Esto va a ser un poco de un revisión de código, pero esperemos que esto le ayudará con su futuro análisis.

  1. Si usted va a crear CSV, yo muy recomendamos que utilice el CSV de la gema.Se garantiza que no tendrá que crear roto CSV (que su código será crear el momento en que uno de los campos contiene una coma).También, le permite ser un poco más descriptivo utilizando los nombres de las variables.

  2. Como ha sido mencionado por otros, no se uso // si usted puede utilizar /.Hay una penalización de rendimiento, además de no ser lo que realmente quieres en algunos casos.

  3. La proliferación de item.parent.parent.parent me dice que fue demasiado lejos hacia abajo por el árbol en su camino.Tome ventaja de XPath de predicados (en []) para asegurarse de que está en el nivel correcto.

  4. Además, siempre estamos tomando ventaja de XPath, que no necesita de un índice o de una break cuando le dices a XPath para no dar más que los 7 primeros.Yo no implementar esto porque dado sus datos no estoy seguro de que si usted lo necesita en absoluto.

Ejemplo:

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

No tiene el elemento raíz CWITEMEXPORT y el niño de la raíz: elemento ItemExportData.Si cambia su XML a:

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

Usted obtiene lo siguiente en su archivo 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

Alternativamente, puede agregar los elementos requeridos después de leer el archivo:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top