0

I have the following simplified XML structure:

<?xml version="1.0" encoding="UTF-8" ?>
<INVOIC02>
<IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
        <TABNAM>EDI_DC40</TABNAM>
    </EDI_DC40>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718221</IDTNR>
            <KTEXT>Orange</KTEXT>
        </E1EDP19>
        <E1EDP19 SEGMENT="1">
            <QUALF>Z03</QUALF>
            <IDTNR>7610400013079</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718160</IDTNR>
            <KTEXT>Pistache</KTEXT>
        </E1EDP19>     
        <E1EDP19 SEGMENT="1">
            <QUALF>003</QUALF>
            <IDTNR>7610400181600</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718228</IDTNR>
            <KTEXT>Strawberry</KTEXT>
        </E1EDP19>
        <E1EDP19 SEGMENT="1">
            <QUALF>003</QUALF>
            <IDTNR>7610400182287</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718221</IDTNR>
            <KTEXT>Orange</KTEXT>
        </E1EDP19>
        <E1EDP19 SEGMENT="1">
            <QUALF>003</QUALF>
            <IDTNR>7610400182218</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718160</IDTNR>
            <KTEXT>Pistache</KTEXT>
        </E1EDP19>
        <E1EDP19 SEGMENT="1">
            <QUALF>003</QUALF>
            <IDTNR>7610400181600</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDP01 SEGMENT="1">
        <MENGE>1.000</MENGE>
        <E1EDP19 SEGMENT="1">
            <QUALF>002</QUALF>
            <IDTNR>000000000000718228</IDTNR>
            <KTEXT>Strawberry</KTEXT>
        </E1EDP19>
        <E1EDP19 SEGMENT="1">
            <QUALF>003</QUALF>
            <IDTNR>7610400182287</IDTNR>
        </E1EDP19>
    </E1EDP01>
    <E1EDS01 SEGMENT="1">
        <SUMID>001</SUMID>
        <SUMME>6</SUMME>
    </E1EDS01>
</IDOC>
</INVOIC02>

I need to sort the <E1EDP01 SEGMENT="1"> segments by their <IDTNR> number with QUALF='002'

So I came up with this XSLT:

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/>

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

<xsl:template match="IDOC">
    <xsl:apply-templates>
        <xsl:sort select="E1EDP19[QUALF='002']/IDTNR"/>
    </xsl:apply-templates>     
</xsl:template>
</xsl:stylesheet>

But in my Output the <E1EDP01> Segments are sorted but are put at the end of my structure:

<?xml version="1.0" encoding="UTF-8"?>
<INVOIC02>
<EDI_DC40 SEGMENT="1">
    <TABNAM>EDI_DC40</TABNAM>
</EDI_DC40>
<E1EDS01 SEGMENT="1">
    <SUMID>001</SUMID>
    <SUMME>6</SUMME>
</E1EDS01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718160</IDTNR>
        <KTEXT>Pistache</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>003</QUALF>
        <IDTNR>7610400181600</IDTNR>
    </E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718160</IDTNR>
        <KTEXT>Pistache</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>003</QUALF>
        <IDTNR>7610400181600</IDTNR>
    </E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718221</IDTNR>
        <KTEXT>Orange</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>Z03</QUALF>
        <IDTNR>7610400013079</IDTNR>
    </E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718221</IDTNR>
        <KTEXT>Orange</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>003</QUALF>
        <IDTNR>7610400182218</IDTNR>
    </E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718228</IDTNR>
        <KTEXT>Strawberry</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>003</QUALF>
        <IDTNR>7610400182287</IDTNR>
    </E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
    <MENGE>1.000</MENGE>
    <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>000000000000718228</IDTNR>
        <KTEXT>Strawberry</KTEXT>
    </E1EDP19>
    <E1EDP19 SEGMENT="1">
        <QUALF>003</QUALF>
        <IDTNR>7610400182287</IDTNR>
    </E1EDP19>
</E1EDP01>
</INVOIC02>

The <E1EDS01> segment comes before the sorted segments but should be at the end. Why is that?

1 Answer 1

1

This happens because you sort everything and non-E1EDP01 elements are going to have empty sort keys. instead, you could force their position, like:

<xsl:template match="IDOC">
    <xsl:apply-templates select="EDI_DC40"/>
    <xsl:apply-templates select="E1EDP01">
        <xsl:sort select="E1EDP19[QUALF='002']/IDTNR"/>
    </xsl:apply-templates>     
    <xsl:apply-templates select="E1EDS01"/>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.