在我的 Xquery 3.1 模块中,我通过模块导入导入了“全局变量”,其中 ( $globalvar:MSGS
) 包含一个 XML 文档,我通常可以通过引用该变量来访问该文档。例如这个
$globalvar:MSGS//vocab[@xml:id="warning"]\n
Run Code Online (Sandbox Code Playgroud)\n\n将返回
\n\n <vocab xml:id="warning">\n <span lang="en">Warning! Your changes (OBJECTID) could not be saved!</span>\n <span lang="fr">Attention, vos modifications (OBJECTID) n\xe2\x80\x99ont pas pu \xc3\xaatre sauvegard\xc3\xa9es\xc2\xa0!</span>\n </vocab>\n
Run Code Online (Sandbox Code Playgroud)\n\n但以下返回一个错误,err:XPDY0002 variable \'$msg\' is not set
指向第 7-8 行:
1. let $collid := $mydoc//collection-id/text()\n2. let $errtitle := \n3. <msg-error-title>\n4. {\n5. let $msg := $globalvar:MSGS/id("warning")/span\n6. return \n7. <en>{replace($msg[@lang="en"]/text(),"OBJECTID",$collid)}</en>, \n8. <fr>{replace($msg[@lang="fr"]/text(),"OBJECTID",$collid)}</fr>\n9. }\n10. </msg-error-title>\n11. return $errtitle\n
Run Code Online (Sandbox Code Playgroud)\n\n但是如果我删除内部let ... return
并直接引用$globalvar:MSGS …
我有一个XML文档,其中包含如下结构的人员列表:
<listPerson>
<person xml:id="John_de_Foo">
<persName>
<forename>John</forename>
<nameLink>de</nameLink>
<surname>Foo</surname>
<role>snake charmer</role>
</persName>
</person>
<person xml:id="John_de_Foo_jr">
<persName>
<forename>John</forename>
<nameLink>de</nameLink>
<surname>Foo</surname>
<genname>junior</genname>
</persName>
</person>
<person xml:id="M_de_Foo">
<persName>
<forename>M</forename>
<nameLink>de</nameLink>
<surname>Foo</surname>
</persName>
</person>
[...]
</listPerson>
Run Code Online (Sandbox Code Playgroud)
我仅提取某些字段并将其与之连接tokenize()
以<fullname>
使用XSL 3.0(其中$ doc =当前文档)创建一个新元素:
<xsl:template match="listPerson/person">
<fullname>
<xsl:value-of select="$p/persName/name, $p/persName/forename, $p/persName/nameLink, $p/persName/surname, $p/persName/addName, $p/persName/genName" separator=" "/>
</fullname>
<xsl:template/>
Run Code Online (Sandbox Code Playgroud)
输出:
<fullname>John de Foo</fullname>
<fullname>John de Foo junior</fullname>
<fullname>M de Foo</fullname>
Run Code Online (Sandbox Code Playgroud)
但是,我想<forename>
通过特定的测试来处理该元素。如果forename/@text
是单个首字母,请添加.
。新结果将输出:
<fullname>John de Foo</fullname>
<fullname>John de Foo junior</fullname>
<fullname>M. de …
Run Code Online (Sandbox Code Playgroud) 在 Xquery 3.1 中,我正在处理表单中的可变参数以搜索匹配的 XML 文档。XML 文档如下所示:
<listBibl xml:id="TC0001" type="collection">
<bibl>
<title type="collection">Bonum universale de apibus</title>
<affiliation corresp="dominican"/>
<author nymRef="thomas_cantipratensis"/>
<location corresp="flanders"/>
<othercontent>....</othercontent>
</bibl>
</listBibl>
Run Code Online (Sandbox Code Playgroud)
用户可以针对、、和提交可选参数xml:id
,它们可以是具有多个值(序列)的参数。affiliation
author
location
如果用户要提交所有参数,则查询可能如下所示:
for $c in $mycollection//listBibl[@xml:id=($params_id)]
where $c/affiliation[@corresp=($params_affil)]
and $c/author[@nymRef=($params_author)]
and $c/location[@corresp=($params_location)]
return $c
Run Code Online (Sandbox Code Playgroud)
但是用户可以将某些参数留空,从而有效地使每个where
语句都是可选的。
我目前可以组合在一起的唯一解决方案是使用一系列if...then...else
语句来解释参数的每个排列。
在 Xpath 或 Xquery 中是否有任何方法来解释参数为空的wildcard
情况?在伪代码中, where*
代表一个想要的通配符:
where $c/affiliation[if ($params_affil)
then @corresp=($params_affil)
else @corresp=* ]
Run Code Online (Sandbox Code Playgroud)
非常感谢。