append data ton an element, based on matching this exact element

孤街醉人 提交于 2019-12-02 07:17:58

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(., '&#9;')[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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!