2

this is my transformation for making every file in a directory into a carousel (bootstrap), I need, however, for the first file (Folio001.xml), the class "active" to be added to the last line of the code

    declare function app:XMLtoHTML-forAll ($node as node(), $model as map(*), $query as xs:string?){
    let $ref := xs:string(request:get-parameter("document", ""))
    let $xml := doc(concat("/db/apps/BookOfOrders/data/edition/",$ref))
    let $xsl := doc("/db/apps/BookOfOrders/resources/xslt/xmlToHtml.xsl")
    let $params :=
    <parameters>

   {for $p in request:get-parameter-names()
    let $val := request:get-parameter($p,())
    where  not($p = ("document","directory","stylesheet"))
    return
       <param name="{$p}"  value="{$val}"/>
   }
</parameters>
        for $doc in collection("/db/apps/BookOfOrders/data/edition")/tei:TEI
            return
            <div class="carousel-item"> {transform:transform($doc, $xsl, $params)} </div>
}; 

Is there an easy way to do this without changing the most of the code? I was thinking of sth. like this:

if(file-name="Folio001.xml") 
       return
        <div class="carousel-item active"> {transform:transform($doc, $xsl, $params)} </div>
 else            
       return
        <div class="carousel-item"> {transform:transform($doc, $xsl, $params)} </div>

THanks in advance, K

2 Answers2

2

Instead of repeating all of the div structure, just move the conditional for the "active" value inside of the @class attribute constructor:

<div> {
  attribute {"class"} {"carousel-item", if (file-name="Folio001.xml") then "active" else () },
"transform:transform($doc, $xsl, $params)"
} </div>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • It does not work ... Upon creation, I get the following error: https://pastebin.com/Bb5T4Xpn The application code is in the second pb! https://pastebin.com/5QgxPi3S The commented part in the app, I know the name sucks, I'll change it later, works, by the way. Any ideas? –  Feb 29 '20 at 08:57
1

The syntax is e.g.

for $doc in collection(...)
return 
  if (document-uri($doc) = '...') 
  then 
    <div class="carousel-item active"> {transform:transform($doc, $xsl, $params)} </div> 
  else 
    <div class="carousel-item"> {transform:transform($doc, $xsl, $params)} </div> 

I have left out the file name/URI part in the string literal as I am not sure what eXist-db returns for documents in a db exactly.

Note that there also is an at clause

for $doc at $pos in collection(...)
return 
  if ($pos = 1)
  then
    <div class="carousel-item active"> {transform:transform($doc, $xsl, $params)} </div>
  else
    <div class="carousel-item"> {transform:transform($doc, $xsl, $params)} </div>

so depending on what you consider the first document and how eXist returns/orders documents this can also be an option.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110