Remove extra group level with XSLT
I need to remove the extra group level in my generated output. My requirements are that I need to split the data in the element <FFF>
every 10 characters. My code almost works, but it fills in an extra group level <FinalRecord>
. Here is my XML file:
ENTRANCE
<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 not in my code? I'm stuck and don't know what to do. I tried insert <xsl:template match="FinalRecord"/>
after <xsl:template match="@*|node()">
to delete the entry <FinalRecord>
in the XML file and it didn't work. I am using XSLT v2.0.
Your feedback is greatly appreciated.
Thank.
source to share
Note:
The item is FinalRecord
processed by copy-copy-pattern because there is no other matching rule.
I tried insert
<xsl:template match="FinalRecord"/>
after<xsl:template match="@*|node()">
to delete entry in XML file and it didn't work.
The deletion is incorrect. You need to process the BUT element, but just not create the same element again.
See this:
<xsl:template match="FinalRecord">
<xsl:apply-templates />
</xsl:template>
source to share