Re: [xsl] constructing arrays in XSLT with templates

Subject: Re: [xsl] constructing arrays in XSLT with templates
From: "Alan Painter alan.painter@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 19 Jun 2021 08:54:04 -0000
Martin,

I'm liking your xpath example more and more, especially for people who like
in-line parsing (for instance, folks who prefer XQuery over XPath).

But I'm wondering what the equivalent would be for conditional map entries.


Supposing that there is an additional optional attribute on the 'book'
element of the original xml, 'out-of-print="true"'.
Furthermore, let's say that the JSON maps in the 'books' array contain an
additional field when a book is out of print.

In XSLT map entries, you can write:

<xsl:map>
      <xsl:map-entry key="'title'"   select="title!string()"              />
      <xsl:map-entry key="'authors'" select="array { ./author!string() }" />

      <xsl:if test="@out-of-print and @out-of-print eq 'true'" >
          <xsl:map-entry key="'out-of-print'" select="true()" />
      </xsl:if>
</xsl:map>

But I'm thinking that there is not a way to express this with the 'map {
... }' form in xpath.

map {
    'title'        : title!string(),
    'authors'      : array { author/string() },
  ( 'out-of-print' : true() )[@out-of-print and @out-of-print eq 'true']
}

Is there any way of doing something like this using in-line xpath?

On Fri, Jun 18, 2021 at 8:50 PM Martin Honnen martin.honnen@xxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> On 18.06.2021 20:45, Martin Honnen martin.honnen@xxxxxx wrote:
> > Of course for the limited sample you could be tempted to inline anything
> > as an XPath 3.1 expression.
>
> Like this:
>
>    <xsl:template match="/">
>      <xsl:sequence
>        select="map {
>                 'books' : array {
>                    books/book ! map {
>                      'title' : title!string(),
>                      'authors' : array { author/string() }
>                    }
>                  }
>                }"/>
>    </xsl:template>

Current Thread