xml分成两半

时间:2012-08-08 10:02:55

标签: xml xpath mule

我想将我的下面的xml拆分成两个部分,每个部分包含两个stream.Product of the 4,是否有任何方法可以使用xpath或xp​​ath-node来实现,我尝试了表达式:

/stream.PurchaseOrder/orderLine/products/stream.Product 

它将产品分为4个部分,任何1可以帮助我用表达式或某些技术来达到要求吗?

<stream.PurchaseOrder>
  <metaData>
    <supplierId>1001</supplierId>
    <supplier>Supplier1</supplier>
  </metaData>
  <orderLine>
    <manufacturer>Manufacturer-1993628836</manufacturer>
    <location>Location-1616142228</location>
    <products>
      <stream.Product>
        <productId>852693979</productId>
        <productName>ProductName-20</productName>
        <price>100</price>
        <model>Model413</model>
        <quantity>50</quantity>
      </stream.Product>
      <stream.Product>
        <productId>1111</productId>
        <productName>ProductName11</productName>
        <price>40</price>
        <model>Model12</model>
        <quantity>150</quantity>
      </stream.Product>
      <stream.Product>
        <productId>85</productId>
        <productName>ProductName3</productName>
        <price>10</price>
        <model>Model3</model>
        <quantity>5</quantity>
      </stream.Product>
      <stream.Product>
        <productId>11</productId>
        <productName>ProductName4</productName>
        <price>4</price>
        <model>Model4</model>
        <quantity>15</quantity>
      </stream.Product>
    </products>
  </orderLine>
</stream.PurchaseOrder>

1 个答案:

答案 0 :(得分:2)

您需要使用count(/stream.PurchaseOrder/orderLine/products/stream.Product)来计算Product元素的数量,然后除以2.使用round函数对其进行舍入,以防元素数量不均匀。然后使用position()函数,如下所示:

上半场:

/stream.PurchaseOrder/orderLine/products/stream.Product[position()<=round(count(/stream.PurchaseOrder/orderLine/products/stream.Product) div 2)]

下半场:

/stream.PurchaseOrder/orderLine/products/stream.Product[position()>round(count(/stream.PurchaseOrder/orderLine/products/stream.Product) div 2)]