使用node.js中的xpath和xmldom替换xml的一部分

时间:2016-08-01 14:57:59

标签: javascript xml node.js xpath xmldom

我有一个巨大的xml结构,我已在下面附上,

我正在添加更新详细信息并将数据添加到产品跟踪模块,但是当我尝试更改它时,它将使用新的xml替换整个xml 示例xml

<query_by_gtin_response:queryByGtinResponse xmlns:query_by_gtin_response="urn:gs1:tsd:query_by_gtin_response:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:gs1:tsd:query_by_gtin_response:xsd:1 QueryByGtinResponse.xsd urn:gs1:tsd:basic_product_information_module:xsd:1 BasicProductInformationModule.xsd urn:gs1:tsd:nutritional_product_information_module:xsd:1 NutritionalProductInformationModule.xsd">
  <productData>
    <gtin>00000000000509</gtin>
    <targetMarket>124</targetMarket>
    <informationProviderGLN>1111111133333</informationProviderGLN>
    <informationProviderName>Testsub</informationProviderName>
    <productDataRecord>
      <module>
        <bpi:basicProductInformationModule xmlns:bpi="urn:gs1:tsd:basic_product_information_module:xsd:1">
          <productName languageCode="en">waka waka</productName>
          <brandNameInformation>
            <brandName>wowozella</brandName>
          </brandNameInformation>
        </bpi:basicProductInformationModule>
      </module>
      <module>
        <nfii:nonfoodIngredientInformationModule xmlns:nfii="urn:gs1:tsd:nonfood_ingredient_information_module:xsd:1"/>
      </module>
      <module>
        <product_tracking_information_module>
          <variantId>ec9ef090-57c2-11e6-bc2b-51c4bf71f613</variantId>
          <status/>
          <name>admin@gmail.com</name>
          <createdBy>552f5b90b348147e03e49b62</createdBy>
          <createdDate>2016-08-01T08:35:38.648Z</createdDate>
          <updatedBy>552f5b90b348147e03e49b62</updatedBy>
          <updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
          <applicationId>webapp</applicationId>
          <history>
            <createdDate>2016-08-01T08:35:38.648Z</createdDate>
            <updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
            <status/>
            <updatedBy>552f5b90b348147e03e49b62</updatedBy>
            <createdBy>552f5b90b348147e03e49b62</createdBy>
            <applicationId>webapp</applicationId>
            <name>admin@gmail.com</name>
          </history>
        </product_tracking_information_module>
      </module>
    </productDataRecord>
  </productData>
</query_by_gtin_response:queryByGtinResponse>

示例xml我想用下面的product_tracking_information_module替换当前的完整数据

<product_tracking_information_module>
  <variantId>ec9ef090-57c2-11e6-bc2b-51c4bf71f613</variantId>
  <status>deleted</status>
  <name>gs1canada</name>
  <createdBy>552f5b90b348147e03e49b62</createdBy>
  <createdDate>2016-08-01T08:35:38.648Z</createdDate>
  <updatedBy>company Id 552f5b62b348147e03e49b61</updatedBy>
  <updatedDate>2016-08-01T14:52:23.985Z</updatedDate>
  <applicationId>serverlet</applicationId>
  <history>
    <createdDate>2016-08-01T08:35:38.648Z</createdDate>
    <updatedDate>2016-08-01T08:35:38.648Z</updatedDate>
    <status/>
    <updatedBy>552f5b90b348147e03e49b62</updatedBy>
    <createdBy>552f5b90b348147e03e49b62</createdBy>
    <applicationId>webapp</applicationId>
    <name>admin@gmail.com</name>
  </history>
  <history status="deleted">
    <applicationId/>
    <updatedBy>company Id 552f5b62b348147e03e49b61</updatedBy>
    <updatedDate>2016-08-01T14:52:23.985Z</updatedDate>
    <name>gs1canada</name>
  </history>
</product_tracking_information_module>

以及执行此操作的代码,其中doc是xmldom解析的Document

var ptimdom = new dom().parseFromString(ptimxml.toString());
var nodesPTIM = xpath.select("//productData//productDataRecord//module//product_tracking_information_module", doc);
doc.replaceChild(ptimdom,nodesPTIM[0]);

但结果是此后的文档仅为第二个xml,并且完整数据丢失。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试nodesPTIM[0].parentNode.replaceChild(ptimdom, nodesPTIM[0]),即在要替换的元素的parentNode上调用replaceChild方法。