Re: [xsl] Making a lookup structure from multiple documents

Subject: Re: [xsl] Making a lookup structure from multiple documents
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Jun 2023 19:55:54 -0000
Based on the resulting <map-data> element, your solution gives me what I
need:



<?xml version="1.0" encoding="UTF-8"?>

<map-data>

   <all>

      <tool quantity="1">Tool1</tool>

      <spare quantity="1">Spare1</spare>

      <spare quantity="3">Spare2</spare>

      <spare quantity="1">Spare3</spare>

      <supply quantity="1">Supply1</supply>

      <tool quantity="1">Tool1</tool>

      <spare quantity="1">Spare1</spare>

      <spare quantity="3">Spare2</spare>

      <spare quantity="1">Spare3</spare>

      <supply quantity="1">Supply1</supply>

   </all>

   <summary>

      <tool quantity="2">Tool1</tool>

      <spare quantity="2">Spare1</spare>

      <spare quantity="6">Spare2</spare>

      <spare quantity="2">Spare3</spare>

      <supply quantity="2">Supply1</supply>

   </summary>

</map-data>



Here is the modified stylesheet:



<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

    xmlns:xs="http://www.w3.org/2001/XMLSchema";

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

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

    exclude-result-prefixes="xs math map"

    version="3.0" expand-text="yes">



    <xsl:output indent="yes"/>



    <xsl:accumulator name="summary-data" initial-value="()" as="element()*">

        <xsl:accumulator-rule
match="*[@href[matches(.,'(dita(map)?|xml)$','i')][doc-available(.)]]">

            <xsl:sequence select="$value"/>

            <xsl:apply-templates select="doc(@href)" mode="collect"/>

        </xsl:accumulator-rule>

        <xsl:accumulator-rule match="tool" select="$value, ." />

        <xsl:accumulator-rule match="spare" select="$value, ." />

        <xsl:accumulator-rule match="supply" select="$value, ." />

    </xsl:accumulator>



    <xsl:mode name="collect" on-no-match="shallow-skip"
use-accumulators="summary-data"/>



    <xsl:template match="/" mode="collect">

        <xsl:apply-templates mode="#current"/>

        <xsl:sequence select="accumulator-after('summary-data')"/>

    </xsl:template>





    <xsl:template match="/">

        <map-data>

            <!--<xsl:apply-templates/>-->

            <all>

                <xsl:for-each select="accumulator-after('summary-data')">

                    <xsl:element name="{local-name(.)}"><xsl:attribute
name="quantity" select="./@quantity"/>{.}</xsl:element>

                </xsl:for-each>

            </all>

            <summary>

                <xsl:for-each-group select="accumulator-after('summary-data')"
group-by=".">

                    <xsl:element name="{local-name(.)}"><xsl:attribute
name="quantity" select="sum(current-group()/@quantity)"/>{.}</xsl:element>

                </xsl:for-each-group>

            </summary>

        </map-data>

    </xsl:template>



    <xsl:mode on-no-match="shallow-skip" use-accumulators="summary-data"/>



</xsl:stylesheet>



Thank you very much Martin! I appreciate your generous help.



Rick

Current Thread