Re: [xsl] Grouping?

Subject: Re: [xsl] Grouping?
From: "Christoph Naber pentium120mhz@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 20 Oct 2020 13:22:57 -0000
Hello Charlie,

I came up with this XSLT2.0 solution:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"; xmlns:fn="my.functions">
    <xsl:output indent="no" method="text" encoding="UTF-8"
media-type="text/plain"/>

<xsl:template match="work_package">
<xsl:apply-templates select="node()">
<xsl:with-param name="result" select="(@id)" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="targetfile[position() = last()]">
<xsl:variable name="prec-sibl-with-childs"
select="preceding-sibling::targetfile[*] | ." />
<xsl:apply-templates select="$prec-sibl-with-childs" mode="generate" />
</xsl:template>


<xsl:template match="targetfile" />

<xsl:template match="targetfile" mode="generate" >
<xsl:param name="result" tunnel="yes" />
<xsl:variable name="first-prec-sibl-with-childs"
select="preceding-sibling::targetfile[*][1]" />

<xsl:variable name="ids" select="
(preceding-sibling::targetfile[
generate-id(.) ne generate-id($first-prec-sibl-with-childs) and
(
every $i in following-sibling::targetfile satisfies (
generate-id($i) ne generate-id($first-prec-sibl-with-childs)
)
)
] | .)/@id" />

<xsl:value-of select="string-join((fn:stripped-seq($result), $ids), ',')" />
<xsl:text>&#xa;</xsl:text>

<xsl:apply-templates select="node()">
<xsl:with-param name="result" select="$result, @id" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="node()" >
<xsl:apply-templates select="@*|node()" />
</xsl:template>

<xsl:function name="fn:stripped-seq">
<xsl:param name="seq" />
<xsl:sequence select="$seq[1]" />
<xsl:for-each select="1 to (count($seq) - 1)" >
<xsl:sequence select="string('')" />
</xsl:for-each>
</xsl:function>
</xsl:stylesheet>

Based on your xml-input the result is

M2183892350419,O1012492350419
M2183892350419,O1003092350419
M2183892350419,,O1012492350419,O1000492350419,O1002492350419
M2183892350419,,,O1012492350419,O1000192350418,O1000192350416,O1000192350417,O1000192350415,O1000192350414
M2183892350419,M2184292350419
M2183892350419,,O1012492350419,M2171392350419
M2183892350419,,,O1012492350419,M2171492350419
M2183892350419,,,,O1012492350419,M2171292350419
M2183892350419,,,M2171192350419
M2183892350419,,,,O1012492350419
M2183892350419,,M2183992350419
M2183892350419,,,O1012492350419,O1003092350419
M2183892350419,,,,O1012492350419,O1000492350419,O1002492350419
M2183892350419,,,O1011492350417
M2183892350419,,,,O1012492350419
M2183892350419,,M2007992350419
M2183892350419,,,O1012492350419,O1003092350419
M2183892350419,,,,O1012492350419,O1000492350419,O1002492350419

This differs only in line 4, where it seems that the last two elements have
been omitted in your example.


Best regards
Christoph



Am Di., 20. Okt. 2020 um 04:40 Uhr schrieb Charlie0 charlieo0@xxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>:

> Greetings,
>
> I have an issue that I think is a grouping problem but I just can't seem
> to figure it out. I have no code to show as I'm that lost on this. Using
> XSLT 2.0 with Oxygen 18.1.
>
> I have the following XML that needs to transform to a comma delimited
> text file. See text after the XML. Any assistance to get me in the right
> direction is appreciated.
>
> XML:
>
> <work_package id="M2183892350419">
>     <eqpcond>
>        <targetfile id="O1012492350419"/>
>     </eqpcond>
>     <eqpcond>
>        <targetfile id="O1003092350419">
>           <targetfile id="O1012492350419"/>
>           <targetfile id="O1000492350419"/>
>           <targetfile id="O1002492350419">
>              <targetfile id="O1012492350419"/>
>              <targetfile id="O1000192350418"/>
>              <targetfile id="O1000192350416"/>
>              <targetfile id="O1000192350417"/>
>              <targetfile id="O1000192350415"/>
>              <targetfile id="O1000192350414"/>
>           </targetfile>
>        </targetfile>
>     </eqpcond>
>     <eqpcond>
>        <targetfile id="M2184292350419">
>           <targetfile id="O1012492350419"/>
>           <targetfile id="M2171392350419">
>              <targetfile id="O1012492350419"/>
>              <targetfile id="M2171492350419">
>                 <targetfile id="O1012492350419"/>
>                 <targetfile id="M2171292350419"/>
>              </targetfile>
>              <targetfile id="M2171192350419">
>                 <targetfile id="O1012492350419"/>
>              </targetfile>
>           </targetfile>
>           <targetfile id="M2183992350419">
>              <targetfile id="O1012492350419"/>
>              <targetfile id="O1003092350419">
>                 <targetfile id="O1012492350419"/>
>                 <targetfile id="O1000492350419"/>
>                 <targetfile id="O1002492350419"/>
>              </targetfile>
>              <targetfile id="O1011492350417">
>                 <targetfile id="O1012492350419"/>
>              </targetfile>
>           </targetfile>
>           <targetfile id="M2007992350419">
>              <targetfile id="O1012492350419"/>
>              <targetfile id="O1003092350419">
>                 <targetfile id="O1012492350419"/>
>                 <targetfile id="O1000492350419"/>
>                 <targetfile id="O1002492350419"/>
>              </targetfile>
>           </targetfile>
> </targetfile></eqpcond></work_package>
>
> Desired output:
>
> M2183892350419,O1012492350419
> M2183892350419,O1003092350419
> M2183892350419,,O1012492350419,O1000492350419,O1002492350419
> M2183892350419,,,O1012492350419,O1000192350418,O1000192350416,O1000192350417
>
>
> M2183892350419,M2184292350419
> M2183892350419,,O1012492350419,M2171392350419
> M2183892350419,,,O1012492350419,M2171492350419
> M2183892350419,,,,O1012492350419,M2171292350419
> M2183892350419,,,M2171192350419
> M2183892350419,,,,O1012492350419
> M2183892350419,,M2183992350419
> M2183892350419,,,O1012492350419,O1003092350419
> M2183892350419,,,,O1012492350419,O1000492350419,O1002492350419
> M2183892350419,,,O1011492350417
> M2183892350419,,,,O1012492350419
> M2183892350419,,M2007992350419
> M2183892350419,,,O1012492350419,O1003092350419
> M2183892350419,,,,O1012492350419,O1000492350419,O1002492350419
>
> Regards,
>
> CharlieO

Current Thread