如何使用不包含属性和元素

How to use not contains with attributes and elements

<newproducts>
        <number id ="1">
        <number id ="2">
        <number id ="3">
        <number id ="4">
        <number id ="5">
</newproducts>    
<products>   
        <number>2</number>
        <number>3</number>
        <number>4</number>
</products>

number id 包含 2, 3, 4 所以输出应该是这样的

<number>1</number>
<number>5</number>

因为1和5不在number id中所以想输出它们

为什么这不起作用?

for $z in /newproducts/number[@id/string() != /products/number/text()]
return 
<number>{$z}</number>

contains 函数或 exists 甚至 if then else 是否可行?

!= 运算符实际上是“序列中是否至少有一项不同”。如果您需要有效地检查“序列中是否没有相同的项目”,请使用:

not(@id/string = /products/number/text())

而不是

@id/string() != /products/number/text()

尝试如下修改(我用根节点包装了您的输入并在此过程中更正了一些拼写错误):

let $list :=
    <list>
    <newproducts>
            <number id ="1"/>
            <number id ="2"/>
            <number id ="3"/>
            <number id ="4"/>
            <number id ="5"/>
    </newproducts>    
    <products>   
            <number>2</number>
            <number>3</number>
            <number>4</number>
    </products>
    </list>
return
for $z in $list/newproducts/number[not(@id/string() = $list/products/number/text())]
return <number>{$z}</number> 

那个returns:

<number>
  <number id="1"/>
</number>
<number>
  <number id="5"/>
</number>

但是您的预期输出没有包含在数字中的数字,因此请将您的 return 子句从

更改为
<number>{$z}</number>  

类似于:

<number>{$z/@id/string()}</number>  

例如:

...
for $z in $list/newproducts/number[not(@id/string() = $list/products/number/text())]
return <number>{$z/@id/string()}</number>  

哪个return

<number>1</number>
<number>5</number>