我想从列表中提取最常用的元素.
该列表$listOut
由此类型的元素组成:
<Outcome>
<Parameter>B</Parameter>
<Value>15</Value>
<MinVal>1</MinVal>
<MaxVal>20</MaxVal>
</Outcome>
<Outcome>
<Parameter>A</Parameter>
<Value>15</Value>
<MinVal>1</MinVal>
<MaxVal>20</MaxVal>
</Outcome>
<Outcome>
<Parameter>D</Parameter>
<Value>43</Value>
<MinVal>34</MinVal>
<MaxVal>36</MaxVal>
</Outcome>
<Outcome>
<Parameter>B</Parameter>
<Value>4</Value>
<MinVal>1</MinVal>
<MaxVal>20</MaxVal>
</Outcome>
Run Code Online (Sandbox Code Playgroud)
我想要获得的是<Parameter>B</Parameter>
,因为参数B存在2次,所以它是最常见的.
我不知道如何做到这一点我不能使用该group by
声明.(仅限For,Let,Order By,Where,Return)
我想过这样的事情:
for $outOk in distinct-values( $listOut )
let $paramOk := //Outcome[Parameter eq $outOk]
order by count( //Outcome[Parameter eq $outOk] )
return $paramOk
Run Code Online (Sandbox Code Playgroud)
但它自然不起作用.
我想从列表中提取第一个元素$res/Name
。在我的解决方案中,我获得了所有列表,而不仅仅是第一个元素,这是我写的。
for $res in /Restaurants/Restaurant
let $n := count($res/Dish)
order by $n descending
return ($res/Name)[1]
Run Code Online (Sandbox Code Playgroud)
您可以在此处看到 XML 和结果:http://www.xpathtester.com/xquery/7992b12910492f493273835e828dc386
哪里有问题?
我想在这个例子中使用 for like 创建一个元素列表:
for $ingr in distinct-values(//Ingredient/Name)
let $res := (
for $res2 in //Restaurant
return if( some $ingr2 in $res2/Dish/Ingredient/Name satisfies $ingr2 eq $ingr )
then( $res2 )
else()
)
return $ingr, $res
Run Code Online (Sandbox Code Playgroud)
我想获得的是:For each ingredient return the list of restaurants that use it
。我真的不明白问题出在哪里?
在这里你可以看到一个例子:http : //www.xpathtester.com/xquery/0c8f9699df404020afd07ff9f9517a46
一直在说: ERROR - Variable $res has not been declared
我已经检查了此解决方案,但在python3中不起作用。
我有一个这样的转义字符串:str = "Hello\\nWorld"
而且我想获得未转义的相同字符串:str_out = Hello\nWorld
我尝试了这个没有成功: AttributeError: 'str' object has no attribute 'decode'
这是我的示例代码:
str = "Hello\\nWorld"
str.decode('unicode_escape')
Run Code Online (Sandbox Code Playgroud)