Re: Processiing arrays in XSLT 4

Am 09.01.2021 um 20:54 schrieb Michael Kay:
> Let's do a use case: converting
>
> <hiistory>
>        <createdby="Michael Kay"on="2016-12-19"/>
> <modifiedby="Michael Kay"on="2017-08-10"change="more precise result
> assertion"/>
> <modifiedby="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>

In the spirit of XSLT template based processing I think this fits in
nicely with existing skills and existing approaches in previous versions
of XSLT.

Received on Sunday, 10 January 2021 10:29:24 UTC