【问题标题】:XQuery - replace empty element with a text value (on output to HTML)XQuery - 用文本值替换空元素(输出到 HTML)
【发布时间】:2018-11-07 14:16:05
【问题描述】:

我在 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,其中使用functx:remove-elements-deep 删除了许多后代节点(即suppliedcorr):

<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"] 替换为“”,将gap 替换为“[ ]”,如下例所示:

<persName  nymRef="#Guilhem_Canast-Brus_MSP-AU" role="own">Willelmi<lb break="y" n="20"/>de Canast Brus</persName>

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

非常感谢您的帮助。

【问题讨论】:

  • 这些空元素是persName 元素的所有子元素还是可以作为嵌套级别的后代?
  • 在某些情况下,他们有可能是persName 的后代,而不仅仅是直系子女...
  • 传入lbgap 等元素名称以及相应的替换是否就足够了,还是需要检查@break = 'y' 等属性或其他条件?
  • 有时必须检查一个属性。我在下面发布了一个有效的 hack,但实际上并不能维护。

标签: xquery xquery-3.0


【解决方案1】:

使用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>

然后我可以像上面一样继续处理。

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多