我尝试过几件事:
for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund')
where $name contains text 'Hans' using fuzzy
return $name | $name
(: returns error: Stopped at line 3, column 20: [XPTY0004]
Union expression: node() expected, xs:string found. :)
for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund')
where $name contains text 'Hans' using fuzzy
return $name and $name
(: returns true :)
for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund')
where $name contains text 'Hans' using fuzzy
return $name, $name
(: returns …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 xquery 中创建类似哈希映射/键值对的结构。我知道 xquery 中存在类似地图的结构:http://www.w3.org/2005/xpath-functions/map/
甚至在撒克逊语中找到了文档:http://www.saxonica.com/html/documentation/functions/map/
但是我不确定如何创建或使用地图。
到目前为止,这是我的代码:
declare namespace map="http://www.w3.org/2005/xpath-functions/map";
let $a := map:map()
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
Cannot find a matching 1-argument function named
{http://www.w3.org/2005/xpath-functions/map}map()
Run Code Online (Sandbox Code Playgroud)
那么我到底如何在 xquery 中使用映射呢?
假设我有一个元素<x>x</x>
和一些空元素(<a/>, <b/>, <c/>)
,我想一次包装第二个内部的第一个元素,结果<c><b><a><x>x</x></a></b></c>
.当我不知道空元素的数量时,我该怎么做呢?
我可以
xquery version "3.0";
declare function local:wrap-up($inner-element as element(), $outer-elements as element()+) as element()+ {
if (count($outer-elements) eq 3)
then element{node-name($outer-elements[3])}{element{node-name($outer-elements[2])}{element{node-name($outer-elements[1])}{$inner-element}}}
else
if (count($outer-elements) eq 2)
then element{node-name($outer-elements[2])}{element{node-name($outer-elements[1])}{$inner-element}}
else
if (count($outer-elements) eq 1)
then element{node-name($outer-elements[1])}{$inner-element}
else ($outer-elements, $inner-element)
};
let $inner-element := <x>x</x>
let $outer-elements := (<a/>, <b/>, <c/>)
return
local:wrap-up($inner-element, $outer-elements)
Run Code Online (Sandbox Code Playgroud)
但有没有办法通过递归来做到这一点,而不是降序和解析,而是提升和构建?
我希望在XQuery中创建一个if
没有else
部分的语句.
例如:
<results>
{
if (5 = 5) then
<foo/>
}
<results>
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我也试过这个:
<results>
{
if (5 = 5) then
<foo/>
else
return
}
<results>
Run Code Online (Sandbox Code Playgroud)
虽然编译,但在运行时不起作用!
我想返回的每时间表L1和L0项目的数量和也警告的数量,也按计划.
这实际上是一个"数量如果"的情况.
我尝试了以下XQuery,它可以很好地计算L1,L0和警告,但会计算所有警告,而不是仅计算所有警告value = "yes"
.
xquery version "3.0";
let $nl := " "
let $quote := """
let $pipe := "|"
let $nodecount := 0
for $profiles in doc("maik test.xml")/PROFILE
for $schedule in $profiles/SCHEDULE
let $schedulename := $schedule/@name
group by $schedulename
return ($nl,
$schedulename, $pipe, "L0 count:", count($schedule/L0),
$pipe, "L0 Warnings:", count($schedule/L0/ATTRIBUTE[@NAME = "Warnings"]/VALUE/string() = "Yes"),
$pipe, "L1 count:", count($schedule/L0/L1),
$pipe, "L1 Warnings:", count($schedule/L0/L1/ATTRIBUTE[@NAME = "Warnings"]/VALUE/string() = "Yes"))
Run Code Online (Sandbox Code Playgroud)
示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<PROFILE name="profile1">
<SCHEDULE name="schedule1">
<L0> …
Run Code Online (Sandbox Code Playgroud) 我有一个$text = "Hello \xc3\xbc\xc3\xa4\xc3\xb6$"
\n我想使用xquery从文本中删除表情符号。我怎样才能做到这一点?
\n预期结果:“你好\xc3\xbc\xc3\xa4\xc3\xb6$”
\n我尝试使用:
\nreplace($text, '[^\\x00-\\xFFFF]', '')\n
Run Code Online (Sandbox Code Playgroud)\n但没有用。
\n提前致谢 :)
\ndata()函数和Xquery中的string()函数有什么实际区别?当我使用它们时,它们似乎都会返回相同的结果.有人可以给我一个简单的例子,说明它们有何不同?
let $dataset :=
<data-set cool="rad">
<!--- This is a comment -->
<pages cool2="rad2" more="i guess">
<page>
<title>Puppy</title>
<content>Puppies are great!</content>
</page>
<page>
<title>Dogs</title>
<content>Dogs are grown up!</content>
</page>
<page>
<title>Puppy</title>
<content>Puppies are great!</content>
</page>
</pages>
</data-set>
return $dataset/string()
(: data() returns the same thing as string() :)
(: return $dataset/data() :)
Run Code Online (Sandbox Code Playgroud) 我的XQuery是:
declare namespace xsd="http://www.w3.org/2001/XMLSchema";
for $schema in xsd:schema
for $nodes in $schema//*,
$attr in $nodes/xsd:element/@name
where fn:contains($attr,'city')
return $attr
Run Code Online (Sandbox Code Playgroud)
返回: name="city" name="city" name="city" name="city" name="city"
当我添加区分值如:
declare namespace xsd="http://www.w3.org/2001/XMLSchema";
for $schema in xsd:schema
for $nodes in $schema//*,
$attr in $nodes/xsd:element/@name
where fn:contains($attr,'city')
return distinct-values($attr)
Run Code Online (Sandbox Code Playgroud)
返回: city city city city city
我只需要返回一个"城市",我该怎么办呢?
问题背景
鉴于每个家庭的类别......
<?xml version="1.0" encoding="UTF-8"?>
<root>
<family>
<categories>
<cat id="1" />
<cat id="2" />
</categories>
</family>
<family>
<categories>
<cat id="3" />
<cat id="5" />
</categories>
</family>
<family>
<categories>
<cat id="3" />
<cat id="5" />
<cat id="6" />
</categories>
</family>
</root>
Run Code Online (Sandbox Code Playgroud)
我想要家庭最常见的类别......
<common-family-category id="3" count="2"/>
<common-family-category id="5" count="2"/>
Run Code Online (Sandbox Code Playgroud)
成功的尝试
我可以通过对每个迭代的id进行分组来实现该结果...
for $family-category-id in //family/categories/cat/@id
count $return-indexes
group by $family-category-id
order by count($return-indexes) descending
where count($return-indexes) > 1
return
<common-family-category id="{$family-category-id}" count="{count($return-indexes)}" />
Run Code Online (Sandbox Code Playgroud)
并且通过迭代每个类别并将id存储在变量中......
for $family-category in //family/categories/cat
let $family-category-id := $family-category/@id …
Run Code Online (Sandbox Code Playgroud) try {
3 div 0
} catch err:XPTY0004{
'typing error'
} catch * {
$err:code || ' ' ||
$err:description || ' ' ||
$err:value || ' ' ||
$err:module || ' ' ||
$err:line-number || ' ' ||
$err:additional
}
Run Code Online (Sandbox Code Playgroud)
在尝试保存文件时,Altova XMLSpy给了我一个错误:未定义的命名空间前缀'err'.
我应该如何首先定义错误以使其工作?
xquery ×10
xquery-3.0 ×10
xml ×7
basex ×2
saxon ×2
count ×1
flwor ×1
recursion ×1
xml-parsing ×1
xpath ×1