Given the following XML as input,
<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="4">
<RECNO>0</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10354</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="3">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10355</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10356</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<SUB_A>Some random data not matched.</SUB_A>
</RECORD>
<RECORD ID="1">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>19837</TOTALS>
</RECORD>
<RECORD ID="0">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10358</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
</RECORD>
</DATA>
</TABLE>
and the following tab separated file:
[yet another value.]\t10358
value i'd like to add\t10355
(another) value i'd like to add\t10357
i used \t, in order to show where the tab exists in the file. i would like to append the data found in the first column, into the element i try to match on, which in this case in NUMBER.
So if NUMBER equals second column, append to it the value found in the first column, using | as a separator.
how one could have the below result, but with keeping the order of the records, sorting on the element Output:
<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="0">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10358 | [yet another value.]</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
</RECORD>
<RECORD ID="1">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357 | (another) value i'd like to add</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>19837</TOTALS>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10356</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<SUB_A>Some random data not matched.</SUB_A>
</RECORD>
<RECORD ID="3">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10355 | value i'd like to add</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="4">
<RECNO>0</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10354</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
</DATA>
</TABLE>
I use Saxon latest, v 9.8
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>
来源:https://stackoverflow.com/questions/46588096/append-data-ton-an-element-based-on-matching-this-exact-element