Re: Processiing arrays in XSLT 4

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?

Michael Kay
Saxonica

> 

Received on Saturday, 9 January 2021 19:54:49 UTC