如何使用 Nokogiri 合并两个 XML 文件 [英] How to combine two XML files with Nokogiri

查看:65
本文介绍了如何使用 Nokogiri 合并两个 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个独立但相关的文件与 Nokogiri 合并.如果ItemNumber"相同,我想结合产品"和产品定价".

I am trying to combine two separate, but related, files with Nokogiri. I want to combine the "product" and "product pricing" if "ItemNumber" is the same.

我加载了文档,但我不知道如何将两者结合起来.

I loaded the documents, but I have no idea how to combine the two.

产品文件:

<Products>
  <Product>
    <Name>36-In. Homeowner Bent Single-Bit Axe Handle</Name>
    <ProductTypeId>0</ProductTypeId>
    <Description>This single bit curved grip axe handle is made for 3 to 5 pound axes. A good quality replacement handle made of American hickory with a natural wax finish. Hardwood handles do not conduct electricity and American Hickory is known for its strength, elasticity and ability to absorb shock. These handles provide exceptional value and economy for homeowners and other occasional use applications. Each Link handle comes with the required wedges, rivets, or epoxy needed for proper application of the tool head.</Description>
    <ActiveFlag>Y</ActiveFlag>
    <ImageFile>100024.jpg</ImageFile>
    <ItemNumber>100024</ItemNumber>
    <ProductVariants>
      <ProductVariant>
        <Sku>100024</Sku>
        <ColorName></ColorName>
        <SizeName></SizeName>
        <SequenceNo>0</SequenceNo>
        <BackOrderableFlag>N</BackOrderableFlag>
        <InventoryLevel>0</InventoryLevel>
        <ColorCode></ColorCode>
        <SizeCode></SizeCode>
        <TaxableFlag>Y</TaxableFlag>
        <VariantPromoGroupCode></VariantPromoGroupCode>
        <PricingGroupCode></PricingGroupCode>
        <StartDate xsi:nil="true"></StartDate>
        <EndDate xsi:nil="true"></EndDate>
        <ActiveFlag>Y</ActiveFlag>
      </ProductVariant>
    </ProductVariants>
  </Product>
</Products>

产品定价字段:

<ProductPricing>
  <ItemNumber>100024</ItemNumber>
  <AcquisitionCost>8.52</AcquisitionCost>
  <MemberCost>10.7</MemberCost>
  <Price>14.99</Price>
  <SalePrice xsi:nil="true"></SalePrice>
  <SaleCode>0</SaleCode>
</ProductPricing>

我希望生成这样的文件:

I am looking to generate a file like this:

<Products>
  <Product>
    <Name>36-In. Homeowner Bent Single-Bit Axe Handle</Name>
    <ProductTypeId>0</ProductTypeId>
    <Description>This single bit curved grip axe handle is made for 3 to 5 pound axes. A good quality replacement handle made of American hickory with a natural wax finish. Hardwood handles do not conduct electricity and American Hickory is known for its strength, elasticity and ability to absorb shock. These handles provide exceptional value and economy for homeowners and other occasional use applications. Each Link handle comes with the required wedges, rivets, or epoxy needed for proper application of the tool head.</Description>
    <ActiveFlag>Y</ActiveFlag>
    <ImageFile>100024.jpg</ImageFile>
    <ItemNumber>100024</ItemNumber>
    <ProductVariants>
      <ProductVariant>
        <Sku>100024</Sku>
        <ColorName></ColorName>
        <SizeName></SizeName>
        <SequenceNo>0</SequenceNo>
        <BackOrderableFlag>N</BackOrderableFlag>
        <InventoryLevel>0</InventoryLevel>
        <ColorCode></ColorCode>
        <SizeCode></SizeCode>
        <TaxableFlag>Y</TaxableFlag>
        <VariantPromoGroupCode></VariantPromoGroupCode>
        <PricingGroupCode></PricingGroupCode>
        <StartDate xsi:nil="true"></StartDate>
        <EndDate xsi:nil="true"></EndDate>
        <ActiveFlag>Y</ActiveFlag>
      </ProductVariant>
    </ProductVariants>
  </Product>
  <ProductPricing>
    <ItemNumber>100024</ItemNumber>
    <AcquisitionCost>8.52</AcquisitionCost>
    <MemberCost>10.7</MemberCost>
    <Price>14.99</Price>
    <SalePrice xsi:nil="true"></SalePrice>
    <SaleCode>0</SaleCode>
  </ProductPricing>
</Products>

这是我到目前为止的代码:

Here is the code I have so far:

require 'csv'
require 'nokogiri'

xml = File.read('lateApril-product-pricing.xml')
xml2 = File.read('lateApril-master-date')

doc = Nokogiri::XML(xml)
doc2 = Nokogiri::XML(xml2)

pricing_data = []
item_number = []

doc.xpath('//ProductsPricing/ProductPricing').each do |file|

  itemNumber = file.xpath('./ItemNumber').first.text
  variant_Price = file.xpath('./Price').first.text

  pricing_data << [ itemNumber, variant_Price ]

  item_number << [ itemNumber ]
end 

puts item_number ## This prints all the item number but i have no idea how to loop through them and combine them with Product XML

doc2.xpath('//Products/Product').each do |file|
  itemNumber = file.xpath('./ItemNumber').first.text #not sure how to write the conditions here since i don't have pricing fields available in this method
end 

推荐答案

试试这个:

require 'nokogiri'

doc1 = Nokogiri::XML(<<EOT)
<Products>
  <Product>
    <Name>36-In. Homeowner Bent Single-Bit Axe Handle</Name>
  </Product>
</Products>
EOT

doc2 = Nokogiri::XML(<<EOT)
<ProductPricing>
  <ItemNumber>100024</ItemNumber>
</ProductPricing>
EOT

doc1.at('Product').add_next_sibling(doc2.at('ProductPricing'))

结果:

puts doc1.to_xml

# >> <?xml version="1.0"?>
# >> <Products>
# >>   <Product>
# >>     <Name>36-In. Homeowner Bent Single-Bit Axe Handle</Name>
# >>   </Product><ProductPricing>
# >>   <ItemNumber>100024</ItemNumber>
# >> </ProductPricing>
# >> </Products>

,当您提出要求时,将示例输入和预期结果输出剥离为绝对的、裸露的、最小值.除此之外的任何事情都会浪费空间、眼睛时间和大脑 CPU.

Please, when you ask, strip the example input and expected resulting output to the absolute, bare, minimum. Anything beyond that wastes space, eye-time and brain CPU.

这是未经测试的代码,但如果我要合并包含 multiple 的两个文件,我将从这里开始节点:

This is untested code, but is where I'd start if I was going to merge two files containing multiple <ItemNumber> nodes:

require 'nokogiri'

doc1 = Nokogiri::XML(<<EOT)
<Products>
  <Product>
    <Name>36-In. Homeowner Bent Single-Bit Axe Handle</Name>
    <ItemNumber>100024</ItemNumber>
  </Product>
</Products>
EOT

doc2 = Nokogiri::XML(<<EOT)
<ProductPricing>
  <ItemNumber>100024</ItemNumber>
</ProductPricing>
EOT

# build a hash containing the item numbers in doc1 for each product
doc1_products_by_item_numbers = doc1.search('Product').map { |product|
  item_number = product.at('ItemNumber').value
  [
    item_number,
    product
  ]
}.to_hash

# build a hash containing the item numbers in doc2 for each product pricing
doc2_products_by_item_numbers = doc2.search('ProductPricing').map { |pricing| 
  item_number = pricing.at('ItemNumber').value
  [
    item_number,
    pricing
  ]
}.to_hash

# append doc2 entries to doc1 after each product based on item numbers
doc1_products_by_item_numbers.keys.each { |k|
  doc1_products_by_item_numbers[k].add_next_sibling(doc2_products_by_item_numbers[k])
}

这篇关于如何使用 Nokogiri 合并两个 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆