Everyone. I am facing an issue when I try to update the map using put() in XSLT3.0. I am using the put function incorrectly, but could any of you guide me through using the map:put() correctly? I verified all the posts for similar requirements that I couldn’t find on this site.
Here is my requirement: I have two files—Journal Lines and Asset Information. The lookup field between the files is Asset_Identifier. When the Asset_Identifier in Journal Lines matches with Asset Information, I needed to get the "Asset Identifier Extension" by adding +1 to the value and Location_ID. If we get the same Asset_Identifier from another Journal Line record, we need to check the latest "Asset Identifier Extension" in the map, add +1 to the value, and create a Journal Line.
If we don't have Asset_Identifier in the Asset Information file when look up, then add 0 to the "Asset Identifier Extension" for the first time. However, if we get the same Asset Identifier again after the entry added with 0, it has to be the same logic as above, which is nothing but adding +1 to the existing value.
Journal Lines File data:[Main Data]
<Journal_Lines>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516195</Journal_Sequence_Number>
<Translated_Debit_Amount>959.75</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>959.75</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>59</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>59</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>95</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>95</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180699</Asset_Identifier>
</Journal_Line>
</Journal_Lines>
Asset Information:[Lookup Data]
<Report_Data>
<Report_Entry>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>4</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Report_Entry>
<Report_Entry>
<Asset_Identifier>180680</Asset_Identifier>
<Asset_Identifier_Extension>14</Asset_Identifier_Extension>
<Location_ID>22_7_802</Location_ID>
</Report_Entry>
</Report_Data>
Expected Output:
<Journal_Lines>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516195</Journal_Sequence_Number>
<Translated_Debit_Amount>959.75</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>959.75</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800:Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>5</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>59</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>59</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800:Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>6</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>95</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>95</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800:Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180699</Asset_Identifier>
<Asset_Identifier_Extension>0</Asset_Identifier_Extension>
<Location_ID>BLANK</Location_ID>
</Journal_Line>
</Journal_Lines>
Map Data:[I am updating map based on the records I am getting in the Journal Lines]
<Report_Data>
<Report_Entry>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>6</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Report_Entry>
<Report_Entry>
<Asset_Identifier>180680</Asset_Identifier>
<Asset_Identifier_Extension>14</Asset_Identifier_Extension>
<Location_ID>22_7_802</Location_ID>
</Report_Entry>
<Report_Entry>
<Asset_Identifier>180699</Asset_Identifier>
<Asset_Identifier_Extension>0</Asset_Identifier_Extension>
<Location_ID>BLK</Location_ID>
</Report_Entry>
</Report_Data>
Here is the XSLT3.0 I made:
<?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:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="saxon"
exclude-result-prefixes="#all"
version="3.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:variable name="All_Assets_From_Variable" as="map(xs:string, element(Asset_Identifier))">
<xsl:map>
<xsl:call-template name="getAssetIdentifierDetails"></xsl:call-template>
</xsl:map>
</xsl:variable>
<xsl:template match="Journal_Lines">
<Journal_Lines>
<xsl:for-each select="Journal_Line">
<Journal_Line>
<xsl:variable name="Key" select="Asset_Identifier"/>
<xsl:copy-of select="*"/>
<xsl:if test="$Key !='' and map:contains($All_Assets_From_Variable,$Key)">
<xsl:variable name="AssetInfo" select="map:get($All_Assets_From_Variable,$Key)"/>
<!--The below variable is to update the new extension and location id in the map for the same key-->
<xsl:variable name="KeyValue">
<Asset_Identifier_Extension><xsl:value-of select="$AssetInfo/Asset_Identifier_Extension + 1"/></Asset_Identifier_Extension>
<Location_ID><xsl:value-of select="$AssetInfo/Location_ID"/></Location_ID>
</xsl:variable>
<!--The below two elements are for the Journal Line records-->
<Asset_Identifier_Extension><xsl:value-of select="$AssetInfo/Asset_Identifier_Extension + 1"/></Asset_Identifier_Extension>
<Location_ID><xsl:value-of select="$AssetInfo/Location_ID"/></Location_ID>
<xsl:variable name="AssetInfoUpdate" select="map:put($All_Assets_From_Variable,$Key,$KeyValue)"/>
</xsl:if>
<xsl:if test="$Key !='' and not(map:contains($All_Assets_From_Variable,$Key))">
<xsl:variable name="KeyValue">
<Asset_Identifier_Extension><xsl:value-of select="0"/></Asset_Identifier_Extension>
<Location_ID><xsl:value-of select="BLK"/></Location_ID>
</xsl:variable>
<Asset_Identifier_Extension><xsl:value-of select="0"/></Asset_Identifier_Extension>
<Location_ID><xsl:value-of select="BLK"/></Location_ID>
<xsl:variable name="AssetInfoUpdate" select="map:put($All_Assets_From_Variable,$Key,$KeyValue)"/>
</xsl:if>
</Journal_Line>
</xsl:for-each>
</Journal_Lines>
</xsl:template>
<xsl:template name="getAssetIdentifierDetails">
<xsl:source-document href="mctx:vars/wd.asset.report" streamable="yes">
<xsl:for-each select="Report_Data/Report_Entry">
<xsl:variable name="row" select="copy-of()"/>
<xsl:variable name="vAsset_Identifier" select="$row/Asset_Identifier"/>
<xsl:map-entry key="xs:string($vAsset_Identifier)">
<Asset_Identifier>
<xsl:copy-of select="$row/Asset_Identifier_Extension"/>
<xsl:copy-of select="$row/Location_ID"/>
</Asset_Identifier>
</xsl:map-entry>
</xsl:for-each>
</xsl:source-document>
</xsl:template>
</xsl:stylesheet>
You can use an accumulator to store a map and then manipulate/change the accumulator (map) value in an accumulator rule; the following has the second document inlined for self-containedness of the example, you can of course change that to read the other document.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:accumulator name="accumulated-assets" as="map(xs:integer, map(xs:string, item()))" initial-value="$asset-map">
<xsl:accumulator-rule phase="end" match="Journal_Line/Asset_Identifier"
select="let $id := xs:integer(.),
$contains := map:contains($value, $id)
return
if ($contains)
then
map:put($value, $id, map:put($value($id), 'Asset_Identifier_Extension', xs:integer($value($id)?Asset_Identifier_Extension) + 1))
else
map:put($value, $id, map { 'Asset_Identifier_Extension' : 0, 'Location_ID' : 'BLK' })"/>
</xsl:accumulator>
<xsl:template match="Journal_Line/Asset_Identifier">
<xsl:next-match/>
<Asset_Identifier_Extension>{accumulator-before('accumulated-assets')(xs:integer(.))?Asset_Identifier_Extension}</Asset_Identifier_Extension>
<Location_ID>{accumulator-before('accumulated-assets')(xs:integer(.))?Location_ID}</Location_ID>
</xsl:template>
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy" use-accumulators="accumulated-assets"/>
<xsl:param name="asset-map" as="map(xs:integer, map(xs:string, item()))"
select="map:merge(
$asset-doc/Report_Data/Report_Entry
!
map { xs:integer(Asset_Identifier) : map:merge((* except Asset_Identifier) ! map:entry(local-name(), string())) }
)"/>
<xsl:param name="asset-doc">
<Report_Data>
<Report_Entry>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>6</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Report_Entry>
<Report_Entry>
<Asset_Identifier>180680</Asset_Identifier>
<Asset_Identifier_Extension>14</Asset_Identifier_Extension>
<Location_ID>22_7_802</Location_ID>
</Report_Entry>
<Report_Entry>
<Asset_Identifier>180699</Asset_Identifier>
<Asset_Identifier_Extension>0</Asset_Identifier_Extension>
<Location_ID>BLK</Location_ID>
</Report_Entry>
</Report_Data>
</xsl:param>
</xsl:stylesheet>
Result
<Journal_Lines>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516195</Journal_Sequence_Number>
<Translated_Debit_Amount>959.75</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>959.75</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>6</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>59</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>59</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180698</Asset_Identifier>
<Asset_Identifier_Extension>7</Asset_Identifier_Extension>
<Location_ID>22_7_702</Location_ID>
</Journal_Line>
<Journal_Line>
<Journal_Sequence_Number>PROCUJ0000516196</Journal_Sequence_Number>
<Translated_Debit_Amount>95</Translated_Debit_Amount>
<Translated_Credit_Amount>0</Translated_Credit_Amount>
<Translated_Debit_Minus_Credit_Amount>95</Translated_Debit_Minus_Credit_Amount>
<Ledger_Account>610800: Other Direct Expenses</Ledger_Account>
<Asset_Identifier>180699</Asset_Identifier>
<Asset_Identifier_Extension>0</Asset_Identifier_Extension>
<Location_ID>BLK</Location_ID>
</Journal_Line>
</Journal_Lines>
Might need some adjustments if supposed to be used with streaming but neither your textual description nor your sample XSLT mention streaming for the primary input document.