0

Sample1.xml

<data><row><id>949459</id><product_id>4119945117</product_id></row>
    <row><id>781351</id><product_id>1009460692</product_id></row>
    <row><id>780163</id><product_id>1009755673</product_id></row>
    <row><id>1017226</id><product_id>1013393868</product_id></row>
    <row><id>1017956</id><product_id>1013393871</product_id></row>
    <row><id>1017310</id><product_id>1013393874</product_id></row>
    <row><id>771708</id><product_id>4388803569</product_id></row>
    <row><id>3270790</id><product_id>1013679270</product_id></row>
    <row><id>775869</id><product_id>1014142699</product_id></row>
    <row><id>1017599</id><product_id>1021870484</product_id></row>
    <row><id>1018789</id><product_id>1021870489</product_id></row>
    <row><id>1017091</id><product_id>1021870491</product_id></row>
    <row><id>1017333</id><product_id>1021870492</product_id></row>
    <row><id>1017630</id><product_id>1021870493</product_id></row>
    <row><id>1017774</id><product_id>1021870495</product_id></row>
    <row><id>1018192</id><product_id>1021870496</product_id></row>
    <row><id>1018725</id><product_id>4408034849</product_id></row>
    <row><id>1017990</id><product_id>1021870498</product_id></row>
    <row><id>1018027</id><product_id>1021870499</product_id></row>
    <row><id>1017166</id><product_id>1021870500</product_id></row>
    <row><id>769120</id><product_id>1032140806</product_id></row>
    <row><id>950336</id><product_id>1035310069</product_id></row>
    </data>

示例2.xml

<productSet>
 <row><product>4388803569</product></row>
 <row><product>4408034289</product></row>
 <row><product>4408034589</product></row>
 <row><product>4408034849</product></row>
 <row><product>4094557957</product></row>
 <row><product>4119945117</product></row>
</productSet>

这是我的 Xquery,通过与示例 2 文件中的 product_id 元素进行比较,返回来自 sample1 XML 文件的所有产品元素值。在这里,我试图检索所有不可用的产品。

<outProduct_10310>
{ for $b in doc("sample1.xml")/data/row,
      $a in doc("sample2.xml")/productSet/row[product != $b/product_id]
  return   
      <op_id> { $b/product_id/text() } </op_id>
  }  </outProduct_10310>

当我将这两个 XML 与等号进行比较但我想检索不匹配的数据时,此代码将返回正确的匹配数据,但如果我运行上面的代码,它会给出以下输出:

<outProduct_10310>
  <op_id>4119945117</op_id>
  <op_id>4119945117</op_id>
  <op_id>4119945117</op_id>
  <op_id>4119945117</op_id>
  <op_id>4119945117</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009460692</op_id>
  <op_id>1009755673</op_id>
  <op_id>1009755673</op_id>
  <op_id>1009755673</op_id>
  <op_id>1009755673</op_id>
  <op_id>1009755673</op_id>
  <op_id>1009755673</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393868</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393871</op_id>
  <op_id>1013393874</op_id>
  <op_id>1013393874</op_id>
  <op_id>1013393874</op_id>
  <op_id>1013393874</op_id>
  <op_id>1013393874</op_id>
  <op_id>1013393874</op_id>
  <op_id>4388803569</op_id>
  <op_id>4388803569</op_id>
  <op_id>4388803569</op_id>
  <op_id>4388803569</op_id>
  <op_id>4388803569</op_id>
  <op_id>1013679270</op_id>
  <op_id>1013679270</op_id>
  <op_id>1013679270</op_id>
  <op_id>1013679270</op_id>
  <op_id>1013679270</op_id>
  <op_id>1013679270</op_id>
  <op_id>1014142699</op_id>
  <op_id>1014142699</op_id>
  <op_id>1014142699</op_id>
  <op_id>1014142699</op_id>
  <op_id>1014142699</op_id>
  <op_id>1014142699</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870484</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870489</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870491</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870492</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870493</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870495</op_id>
  <op_id>1021870496</op_id>
  <op_id>1021870496</op_id>
  <op_id>1021870496</op_id>
  <op_id>1021870496</op_id>
  <op_id>1021870496</op_id>
  <op_id>1021870496</op_id>
  <op_id>4408034849</op_id>
  <op_id>4408034849</op_id>
  <op_id>4408034849</op_id>
  <op_id>4408034849</op_id>
  <op_id>4408034849</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870498</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870499</op_id>
  <op_id>1021870500</op_id>
  <op_id>1021870500</op_id>
  <op_id>1021870500</op_id>
  <op_id>1021870500</op_id>
  <op_id>1021870500</op_id>
  <op_id>1021870500</op_id>
  <op_id>1032140806</op_id>
  <op_id>1032140806</op_id>
  <op_id>1032140806</op_id>
  <op_id>1032140806</op_id>
  <op_id>1032140806</op_id>
  <op_id>1032140806</op_id>
  <op_id>1035310069</op_id>
  <op_id>1035310069</op_id>
  <op_id>1035310069</op_id>
  <op_id>1035310069</op_id>
  <op_id>1035310069</op_id>
  <op_id>1035310069</op_id>
</outProduct_10310>

我对 xquery 完全陌生。谁能帮助我如何获取不匹配的数据?

4

2 回答 2

2

首先,您不需要使用 for 循环比较所有 id 对,因为如果您提供序列,标准 = 运算符将搜索相等的值。

因此,查找所有匹配产品的更简单方法是:

<outProduct_10310>
{ for $b in doc("sample1.xml")/data/row[product_id = doc("sample2.xml")/productSet/row/product]
  return   
      <op_id> { $b/product_id/text() } </op_id>
  }  
</outProduct_10310>

对于不匹配的产品,您可以只选择与 not 运算符不匹配的所有产品:(请注意,您不能在此处使用 !=,因为总会有不同的 id)

<outProduct_10310>
{ for $b in doc("sample1.xml")/data/row[not (product_id = doc("sample2.xml")/productSet/row/product) ]
  return   
      <op_id> { $b/product_id/text() } </op_id>
  }  
</outProduct_10310>

根据您的 XQuery 引擎有多好,您可能希望将括号doc("sample2.xml")/productSet/row移出[..]并将其存储在一个变量中。

于 2012-08-21T14:23:07.997 回答
1

请注意,所有一般比较(=、!=、...)都将左操作数的所有项目与右操作数的所有项目进行比较。一旦比较成功(即产量true),结果也将是正确的。例如,(1,2) != (1,2)将 return true,因为单一比较1 != 2产生true

在您的特定查询中,您可以尝试not(product = $b/product_id)改为,只有在没有单个比较产生 true 时才会返回 true。

于 2012-08-21T14:17:14.177 回答