xsl-list
[Top] [All Lists]

Re: [xsl] constructing arrays in XSLT with templates

2021-06-19 03:54:14
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(_dot_)honnen(_at_)gmx(_dot_)de <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

On 18.06.2021 20:45, Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de 
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>



--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>