-1

I have a very poorly structured XML source coming out of our ERP system, and the Parent/Child relationship for Header to Detail is reversed because at the runtime, the parent (key) isn't known until the first detail record is process. This is due to the way the interface from an old DB2 mainframe application was written. Unfortunately, I am not at liberty to modify the report, nor do I have any sway to getting the report changed, so I have to deal with the source as is.

Here is what the XML output looks like:

<report>
    <detail>
        <header>
            <keyfield>1231</keyfield>
            <headerfield1>foo</headerfield>
            <headerfield2>bar</headerfield2>
        </header>
        <keymatch>1231</keymatch>
        <detailfield0>Line1</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1231</keymatch>
        <detailfield0>Line2</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1231</keymatch>
        <detailfield0>Line3</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1231</keymatch>
        <detailfield0>Line4</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1231</keymatch>
        <detailfield0>Line5</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <header>
            <keyfield>1232</keyfield>
            <headerfield1>boo</headerfield>
            <headerfield2>far</headerfield2>
        </header>
        <keymatch>1232</keymatch>
        <detailfield0>Line1</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1232</keymatch>
        <detailfield0>Line2</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1232</keymatch>
        <detailfield0>Line3</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1232</keymatch>
        <detailfield0>Line4</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
    <detail>
        <keymatch>1232</keymatch>
        <detailfield0>Line5</detailfield0>
        <detailfield1>this is bad structure</detailfield1>
        <detailfield2>and not my design</detailfield2>
    </detail>
</report>

Ultimately, I need it to be in a format similar to the following:

<report>
    <header>
        <keyfield>1231</keyfield>
        <headerfield1>foo</headerfield>
        <headerfield2>bar</headerfield2>
            <detail>
                <keymatch>1231</keymatch>
                <detailfield0>Line1</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1231</keymatch>
                <detailfield0>Line2</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1231</keymatch>
                <detailfield0>Line3</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1231</keymatch>
                <detailfield0>Line4</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1231</keymatch>
                <detailfield0>Line5</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
    </header>
    <header>
        <keyfield>1232</keyfield>
        <headerfield1>boo</headerfield>
        <headerfield2>far</headerfield2>
            <detail>
                <keymatch>1232</keymatch>
                <detailfield0>Line1</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1232</keymatch>
                <detailfield0>Line2</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1232</keymatch>
                <detailfield0>Line3</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1232</keymatch>
                <detailfield0>Line4</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
            <detail>
                <keymatch>1232</keymatch>
                <detailfield0>Line5</detailfield0>
                <detailfield1>this is bad structure</detailfield1>
                <detailfield2>and not my design</detailfield2>
            </detail>
    </header>
</report>

Admittedly, I don't know much about XSLT but I'm a quick study, and I've figured out that this is the tool that I need to use. For what it's worth, this is JD Edwards 9.1, and I'll be using BI Publisher to send the data through an XSL Template for further handoff to an EDI Partner.

Thanks,

Justin

J. Ellis
  • 13
  • 4
  • 2
    This is a grouping question. The exact answer depends on whether your processor uses XSLT 1.0 or 2.0. And it's probably the most often asked question here - so do a search for it. – michael.hor257k Jan 14 '16 at 20:22
  • Michael, I have done a search for it. And in none of the results did I find a case where the data is structured like this, but it's still a foreign topic to me. The system uses XSLT 2.0. Thanks so much for your insightful help. – J. Ellis Jan 14 '16 at 21:26

1 Answers1

0

Try it this way:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/report">
    <xsl:copy>
        <xsl:for-each-group select="detail" group-starting-with="detail[header]">
            <header>
                <xsl:copy-of select="header/*"/>
                <xsl:apply-templates select="current-group()"/>
            </header>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

<xsl:template match="detail">
    <xsl:copy>
        <xsl:copy-of select="* except header"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Michael, this worked perfectly. I know it probably feels like spoon feeding to you, but XSLT really is brand new to me and I sincerely appreciate the time you took to point me in the right direction. Hope I can return the favor some day! – J. Ellis Jan 19 '16 at 14:53