XQuery - заменить пустой элемент текстовым значением (при выводе в HTML) - PullRequest
0 голосов
/ 07 ноября 2018

У меня есть такие узлы в коллекции документов 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, corr), удаленных с использованием functx: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"] на "", а gap на "[]", как в этом примере:

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

Я смотрел на functx:replace-element-values, но не мог определить, как его интегрировать.

Большое спасибо за любую помощь.

1 Ответ

0 голосов
/ 07 ноября 2018

Используя заменить значение элемента на 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>

Который я могу затем продолжить обработку, как указано выше.

...