Re: Processiing arrays in XSLT 4

On Sat, Jan 9, 2021 at 11:54 AM Michael Kay <mike@saxonica.com> wrote:

> Let's do a use case: converting
>
> <hiistory>
>       <created by="Michael Kay" on="2016-12-19"/>
>       <modified by="Michael Kay" on="2017-08-10" change="more precise
> result assertion"/>
>       <modified by="Michael Kay" on="2019-12-02" change="Test wrongly
> assumed that document order was predictable.'"/>
> </history>
>
> to JSON:
>
> [
>       { "event": "created",
>         "by" : "Michael Kay",
>        "on" : "2016-12-19" },
>
>        {"event" : "modified:
>         "by" : "Michael Kay",
>         "on" : "2017-08-10",
>         "change" : "more precise result assertion" },
>
>        {"event" : "modified:
>         "by" : "Michael Kay",
>         "on" : "2019-12-02",
>         "change" : "est wrongly assumed that document order was
> predictable." }
> ]
>
> I want to do this using template rules. Something like this:
>
> <xsl:output method="json"/>
>
> <xsl:template match="history">
>    <xsl:array>
>       <xsl:apply-templates/>
>    </xsl:array>
> </xsl:template>
>
> <xsl:template match="history/*">
>    <xsl:array-member>
>       <xsl:map>
>           <xsl:map-entry key="'event'" select="local-name()"/>
>           <xsl:apply-templates select="@*"/>
>       </xsl:map>
>   </xsl:array-member>
> </xsl:template>
>
> <xsl:template match="@*">
>    <xsl:map-entry key="local-name()" select="."/>
> </xsl:template>
>
> How would you want to write this?
>


I would prefer the proposed syntax, providing a member-generator function
as the value of the member-generator attribute:

  <xsl:output method="json"/>

  <xsl:template match="history">
     <xsl:array member-source="*"  member-generator="my:mapHistory"/>
  </xsl:template>

  <xsl:function name="my:mapHistory" as="item()*">
     <xsl:param name="pIndex" as="xs:integer"/>
     <xsl:param name="pMemberSource" as="element()"/>

     <xsl:variable name="vMember" select="$pMemberSource [$pIndex]"/>

     <xsl:sequence select=
      "map:merge(
                            (map-entry('event' : $vMember/local-name(),
                             for $mem in $vMember/@*
                               return
                                    map:entry($mem/local-name(),
$mem/string())
                            )
                         )
      "/>
  </xsl:function>


Thanks,
Dimitre


>
> Michael Kay
> Saxonica
>
>
>
>

-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Received on Saturday, 9 January 2021 20:46:27 UTC