The xsl:merge
is similar to your previous question and to sort the merge result you can simply wrap the xsl:merge
into an xsl:perform-sort
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
expand-text="yes" version="3.0">
<xsl:param name="text-uri" as="xs:string">test2017100602.txt</xsl:param>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="lines" as="element(line)*">
<xsl:apply-templates select="unparsed-text-lines($text-uri)"/>
</xsl:variable>
<xsl:template match=".[. instance of xs:string]">
<xsl:variable name="tokens" as="xs:string*" select="tokenize(., '	')[normalize-space()]"/>
<line number="{$tokens[2]}">{$tokens[1]}</line>
</xsl:template>
<xsl:template match="TABLE/DATA">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:perform-sort>
<xsl:sort select="xs:integer(@ID)"/>
<xsl:merge>
<xsl:merge-source name="record" select="RECORD">
<xsl:merge-key select="NUMBER"/>
</xsl:merge-source>
<xsl:merge-source name="line" select="$lines" sort-before-merge="yes">
<xsl:merge-key select="@number"/>
</xsl:merge-source>
<xsl:merge-action>
<xsl:if test="current-merge-group('record')">
<xsl:copy>
<xsl:apply-templates select="@*, NUMBER/preceding-sibling::*"/>
<NUMBER>
<xsl:value-of select="NUMBER, current-merge-group()[2]"
separator=" | "/>
</NUMBER>
<xsl:apply-templates select="NUMBER/following-sibling::*"/>
</xsl:copy>
</xsl:if>
</xsl:merge-action>
</xsl:merge>
</xsl:perform-sort>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>