XQuery-用文本值替换空元素(在输出到HTML上)

时间:2018-11-07 14:16:05

标签: xquery xquery-3.0

我在xml:tei文档集合中有这样的节点:

[...]
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAdo #pAud" role="par">Willelmum de Canast-Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAdo #pAud #pPax" role="par">Willelmum de Canast-Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi de Canast-Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAdo" role="par">W<supplied reason="expname">illelmum</supplied> de Canast</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi<lb break="y" n="20"/>de Canast Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAdo #pAud #pPax" role="par">Willelmum de<lb break="y" n="22"/>Canast</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAdo" role="par">Willelmum de Canast Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi de Canast-Brus</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#pAud #pAdo" role="par">W<supplied reason="expname">illelmum</supplied> de Canast</persName>
<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" ana="#nAdo" role="par">W<supplied reason="expname">illelmum</supplied> de Canast Bru</persName>
[...]

XQuery 3.1中的以下查询:

let $a := 
  <div>
   {let $x := functx:remove-elements-deep(collection($coll)//tei:persName[@nymRef="#Guilhem_Canast-Brus_MSP-AU"][text()],("supplied","corr","del"))
    for $y in $x
    let $z := normalize-space(string-join(replace($y,",","")))
      group by $z
      order by $z ascending
      return  <span>
              {$z}
              </span>
}</div>
return $a

返回以下HTML,其中包含使用supplied删除的多个后代节点(即corrfunctx:remove-elements-deep):

<div>
  <span>R de Canast</span>
  <span>W</span>
  <span>W Bru</span>
  <span>W Bru de Canast</span>
  <span>W Canast Bru</span>
  <span>W de Canast</span>
  <span>W de Canast Bru</span>
  <span>W de Canast Brus</span>
  <span>W de Canast qui dicitur Lo Brus</span>
  <span>W de Canast- Bru</span>
  <span>W de Canast-Bru</span>
  <span>W de Canast-Brus</span>
  <span>W de CanastBru</span>
  <span>W de CanastBrus</span>
  <span>Willelmi</span>
  <span>Willelmi Canast-Bru</span>
  <span>Willelmi de Canast</span>
  <span>Willelmi de Canast Bru</span>
  <span>Willelmi de Canast Brus</span>
  <span>Willelmi de Canast iunioris</span>
  <span>Willelmi de Canast qui dicitur Brus</span>
  <span>Willelmi de Canast-Brus</span>
  <span>Willelmi de CanastBru</span>
  <span>Willelmi de Canastle Bru</span>
  <span>Willelmide Canast Brus</span>
  <span>Willelmide Canast-Brus</span>
  <span>Willelmo de Canast</span>
  <span>Willelmum de Canast</span>
  <span>Willelmum de Canast Brus</span>
  <span>Willelmum de Canast-Brus</span>
  <span>Willelmum deCanast</span>
  <span>Willelmus de Canast</span>
</div>

但是,我想用字符串替换几个(空)元素。例如,将lb[@break="y"]替换为“”,将{{11}}替换为“ []”,如下例所示:

gap

我当时在看functx:replace-element-values,但无法确定如何集成它。

非常感谢您的协助。

1 个答案:

答案 0 :(得分:0)

使用replace value of element by xquery,我设法提出了一个建议。

declare namespace local = "http://example.org";

declare function local:copy-replace($element as element()) {
  if ($element/self::lb[@break eq "y"])
  then " "
  else if ($element/self::gap)
  then "[  ]"
  else element {node-name($element)}
           {$element/@*,
            for $child in $element/node()
            return if ($child instance of element())
                   then local:copy-replace($child)
                   else $child
           }
   };

   local:copy-replace(<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi<lb break="y" n="20"/>de Can<gap/>t Brus</persName>)

返回:

<persName nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi de Can[  ]t Brus</persName>

然后我可以继续进行上述操作。