xsl-list
[Top] [All Lists]

Re: [xsl] constructing arrays in XSLT with templates

2021-06-19 04:25:45

Am 19.06.2021 um 10:54 schrieb Alan Painter 
alan(_dot_)painter(_at_)gmail(_dot_)com:
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?


My first instinct would be that the data is more consistent if you just do


  <xsl:template match="/">
    <xsl:sequence
      select="map {
               'books' : array {
                  books/book ! map {
                    'title' : title!string(),
                    'authors' : array { author/string() },
                    'out-of-print' : @out-of-print = 'true'
                  }
                }
              }"/>
  </xsl:template>


i.e. give any map representing a book a property of the name out-of-print with the right value.


Otherwise I think you can use map:merge


  <xsl:template match="/">
    <xsl:sequence
      select="map {
               'books' : array {
                  books/book !
                    map:merge((
                      map {
                        'title' : title!string(),
                        'authors' : array { author/string() }
                      },
                      @out-of-print ! map:entry(local-name(), boolean(.)))
                    )
                  }
              }"/>
  </xsl:template>


with xmlns:map="http://www.w3.org/2005/xpath-functions/map";

--~----------------------------------------------------------------
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>