How to update map using map:put() function in XSLT3.0 for XML data?

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>

1

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật