Re: [xsl] Using sibling value in streaming mode

Subject: Re: [xsl] Using sibling value in streaming mode
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 31 Aug 2019 06:58:28 -0000
Am 30.08.2019 um 23:18 schrieb Martynas JuseviD
ius martynas@xxxxxxxxxxxxx:
Hi,

I've started looking into streaming recently (using Saxon 9.9). I have
a use case like this:

Input:

<array>
     <map>
        <string key="key1">value1</string>
        ...
        <string key="id">123456789</string>
        ...
        <string key="keyN">valueN</string>
     </map>
     ...
</array>

Required output:

<items>
     <item>
        <id>123456789</id>
        <key>key1<key>
        <val>value1</val>
     </item>
     ...
     <item>
        <id>123456789</id>
        <key>id<key>
        <val>123456789</val>
     </item>
     ...
     <item>
        <id>123456789</id>
        <key>keyN<key>
        <val>valueN</val>
     </item>
     ...
</items>

The value of <string key="id"> is used as <id> in <item> elements. The
problem is that <string key="id"> can occur in any position in the
<map>.



Is it possible to have a streaming solution in this case?


I think the least buffering is possible with xsl:iterate where you accumulate values in a parameter until you hit the id, then you push the accumulated values out to transform them to "item" elements and for the rest of the iteration you have the id and can always push the current element:


<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="3.0" B B B xmlns:xs="http://www.w3.org/2001/XMLSchema"; B B B xmlns:map="http://www.w3.org/2005/xpath-functions/map"; exclude-result-prefixes="#all" B B B expand-text="yes">

B B B <xsl:output indent="yes"/>

B B B <xsl:mode streamable="yes" on-no-match="shallow-skip"/>

B B B  <xsl:template match="array/map">
B B B B B B B  <items>
B B B B B B B B B B B  <xsl:iterate select="*">
B B B B B B B B B B B B B B B  <xsl:param name="map-id" as="xs:string?"
select="()"/>
B B B B B B B B B B B B B B B  <xsl:param name="values" as="tuple(key:
xs:string, val:
xs:string)*" select="()"/>

B B B B B B B B B B B B B B B  <xsl:variable name="current-value"
as="tuple(key:
xs:string, val: xs:string)"
B B B B B B B B B B B B B B B B B B B  select="map{ 'key' : string(@key),
'val' : string()
}"/>

B B B B B B B B B B B B B B B  <xsl:variable name="found-id" as="xs:boolean"
select="$current-value?key = 'id'"/>

B B B B B B B B B B B B B B B <xsl:apply-templates
B B B B B B B B B B B B B B B B B B B select="
B B B B B B B B B B B B B B B B B B B B B B B if ($found-id)
B B B B B B B B B B B B B B B B B B B B B B B then
B B B B B B B B B B B B B B B B B B B B B B B B B B B $values
B B B B B B B B B B B B B B B B B B B B B B B else
B B B B B B B B B B B B B B B B B B B B B B B B B B B ($values,
$current-value)[$map-id]"
B B B B B B B B B B B B B B B B B B B mode="item">
B B B B B B B B B B B B B B B B B B B <xsl:with-param name="map-id"
B B B B B B B B B B B B B B B B B B B B B B B select="
B B B B B B B B B B B B B B B B B B B B B B B B B B B if ($found-id)
B B B B B B B B B B B B B B B B B B B B B B B B B B B then
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B $current-value?val
B B B B B B B B B B B B B B B B B B B B B B B B B B B else
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B $map-id"
B B B B B B B B B B B B B B B B B B B />
B B B B B B B B B B B B B B B </xsl:apply-templates>


B B B B B B B B B B B B B B B <xsl:next-iteration>
B B B B B B B B B B B B B B B B B B B <xsl:with-param name="map-id"
B B B B B B B B B B B B B B B B B B B B B B B select="
B B B B B B B B B B B B B B B B B B B B B B B B B B B if ($found-id) then
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B $current-value?val
B B B B B B B B B B B B B B B B B B B B B B B B B B B else
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B $map-id"/>
B B B B B B B B B B B B B B B B B B B <xsl:with-param name="values"
B B B B B B B B B B B B B B B B B B B B B B B select="
B B B B B B B B B B B B B B B B B B B B B B B B B B B if ($found-id or
$map-id) then
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B ()
B B B B B B B B B B B B B B B B B B B B B B B B B B B else
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B ($values,
$current-value)"
B B B B B B B B B B B B B B B B B B B />
B B B B B B B B B B B B B B B </xsl:next-iteration>
B B B B B B B B B B B </xsl:iterate>
B B B B B B B </items>
B B B </xsl:template>


B B B  <xsl:template match=".[. instance of tuple(key: xs:string, val:
xs:string)]" mode="item">
B B B B B B B  <xsl:param name="map-id"/>
B B B B B B B  <item>
B B B B B B B B B B B  <id>{$map-id}</id>
B B B B B B B B B B B  <key>{?key}</key>
B B B B B B B B B B B  <val>{?val}</val>
B B B B B B B  </item>
B B B  </xsl:template>

</xsl:stylesheet>

Like the previous example it requires using the Saxon 9 EE specific
syntax extension but you could of course use the same `xsl:iterate`
approach to store and push normal maps or even `copy-of()`ed "string"
elements.

Current Thread