0

我想将下面的 xml 拆分为两个部分,每个部分包含两个流。包含的 4 个的产品,有没有什么方法可以使用 xpath 或 xpath-node 来实现这一点,我尝试了表达式:

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

它将产品分为 4 个部分,any1 可以帮助我表达或某种技术来实现要求吗?

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

1 回答 1

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)]
于 2012-08-08T10:13:30.463 回答