在Marklogic Xquery中将字符串转换为元素



我有以下XQuery脚本,基本上是向MarkLogic中的现有文档添加属性:

xquery version "1.0-ml";
declare namespace xhtml = "http://www.w3.org/1999/xhtml";
declare namespace error="http://marklogic.com/xdmp/error";
declare namespace cdata = "http://www.w3.org/1999/cdata";
declare variable $uri as xs:string external;
declare variable $contentproperties as xs:string external;
xdmp:document-add-properties($uri, ( $contentproperties ) );

外部c#程序调用该查询,并将以下内容作为$contentproperties的字符串传入:

<Title>My Title</Title>,<Date>1629076218</Date>,<SubjectArea>My subject</SubjectArea>,<ContentType>My content type</ContentType>,<Summary>My summary</Summary>

但是,返回一个错误,提到:

xdmp - argtype: (err:XPTY0004) xdmp:document-add-properties(", "My Title,1629076218,My…")——arg2不是element类型()*">

如何将xs:string转换为element()*类型?

document-add-properties方法的引用:https://docs.marklogic.com/xdmp:document-add-properties

您可以tokenize()该字符串的逗号分隔值,然后使用xdmp:unquote()将XML字符串解析为XML文档,然后XPath到元素。将该元素序列赋给另一个变量,并使用该变量设置属性:

xquery version "1.0-ml";
declare namespace xhtml = "http://www.w3.org/1999/xhtml";
declare namespace error="http://marklogic.com/xdmp/error";
declare namespace cdata = "http://www.w3.org/1999/cdata";
declare variable $uri as xs:string external := "test";
declare variable $contentproperties as xs:string external := "<Title>My Title</Title>,<Date>1629076218</Date>,<SubjectArea>My subject</SubjectArea>,<ContentType>My content type</ContentType>,<Summary>My summary</Summary>";
declare variable $contentproperties-element as element()* := tokenize($contentproperties, ",") ! xdmp:unquote(.)/*;
xdmp:document-add-properties($uri, ( $contentproperties-element ) );

最新更新