3

I need to remove the extra group level in my generated output. My requirements is that, I need to split the data in element <FFF> for every 10 characters. My code is almost working but it populated an extra group level <FinalRecord>. Here is my XML File:

INPUT

<RootParent>
<Data>
    <DetailRecord>
        <AAA>6</AAA>
        <BBB>22</BBB>
        <CCC>000000CC</CCC>
    </DetailRecord>
    <FinalRecord>
        <FFF>This is only for testing. I repeat. This is only for testing</FFF>
    </FinalRecord>
</Data>
</RootParent>

GENERATED OUTPUT

<RootParent>
<Data>
    <DetailRecord>
        <AAA>6</AAA>
        <BBB>22</BBB>
        <CCC>000000CC</CCC>
    </DetailRecord>
    <FinalRecord>
        <FinalRecord>
            <FFF>This is on</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>ly for tes</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>ting. I re</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>peat. This</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF> is only f</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>or testing</FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>. This is </FFF>
        </FinalRecord>
        <FinalRecord>
            <FFF>only</FFF>
        </FinalRecord>
    </FinalRecord>
</Data>
</RootParent>

EXPECTED OUTPUT

<RootParent>
<Data>
    <DetailRecord>
        <AAA>6</AAA>
        <BBB>22</BBB>
        <CCC>000000CC</CCC>
    </DetailRecord>
    <FinalRecord>
        <FFF>This is on</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>ly for tes</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>ting. I re</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>peat. This</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF> is only f</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>or testing</FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>. This is </FFF>
    </FinalRecord>
    <FinalRecord>
        <FFF>only</FFF>
    </FinalRecord>
</Data>
</RootParent>

MY XSLT CODE

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="FFF">
    <xsl:analyze-string regex=".{{10}}" select=".">
        <xsl:matching-substring>
            <FinalRecord>
                <FFF>
                    <xsl:value-of select="."/>
                </FFF>
            </FinalRecord>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <FinalRecord>
                <FFF>
                    <xsl:value-of select="."/>
                </FFF>
            </FinalRecord>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>

Is there something wrong or missing in my code? I'm stuck and don't know what to do. I tried to insert the <xsl:template match="FinalRecord"/> after the <xsl:template match="@*|node()"> to delete the record <FinalRecord> in the XML file and it didn't work. I am using the XSLT v2.0.

Your feedback is much appreciated.

Thank you.

1 Answers1

3

Note:

Element FinalRecord is processed by the "identity-copy-template" because there is no other matching rule.


I tried to insert the <xsl:template match="FinalRecord"/> after the <xsl:template match="@*|node()"> to delete the record in the XML file and it didn't work.

Deleting is kind of wrong. You need to process the element BUT just without creating the same element once again.

See this:

<xsl:template match="FinalRecord">
    <xsl:apply-templates />
</xsl:template>
uL1
  • 2,117
  • 2
  • 17
  • 28