Home > database >  Array parsing and matching with values in an XML node (XSLT 1.0)
Array parsing and matching with values in an XML node (XSLT 1.0)

Time:07-14

Allow me to pose here a problem that I am currently encountering:

I have two files:

  1. An XML file containing tags representing operations (historical)
  2. A specific label file for certain operations (the common between the 2 files is the operation reference)

I load the label file into my xslt and iterate through the array to build the output. I make a test to know if the reference of the current node is found in the label file so I recover the label located in the file for display. Here is my code (the loop part):

<xsl:variable name="file-extrait-labels" select="'../../../../../../../../Ressources-bank/xml-xsl/EXTRAIT-10765280004-01102021-31122021.xml'" />
<xsl:variable name="table-extrait-labels" select="document($file-extrait-labels)/transactions-reals-labels" />
<table-movement struct="table">         
     <xsl:for-each select="/ACCOUNT_EXTRACTION/MOVEMENT">
        <ligne struct="row">
            <mvt.date><xsl:value-of select="normalize-space (@MOVEMENT_DATE)" /></mvt.date>
            <mvt.ref><xsl:value-of select="normalize-space (@MOVEMENT_ACCOUNTING_RECORD)" /></mvt.ref>
 
            <xsl:choose>
                <xsl:when test="$table-extrait-labels/transaction[reference = @MOVEMENT_ACCOUNTING_RECORD]">
                    <mvt.lib>
                       <xsl:value-of select="normalize-space ($table-extrait-labels/transaction[reference = @MOVEMENT_ACCOUNTING_RECORD]/label)"/>
                    </mvt.lib>
                </xsl:when>
 
                <xsl:otherwise>
                    <mvt.lib><xsl:value-of select="normalize-space (@MOVEMENT_DESCRIPTION)" /></mvt.lib>
                    <xsl:choose>
                        <xsl:when test="LABELS">
                            <mvt.label><xsl:value-of select="concat(' - ', normalize-space (LABELS[1]/@LABEL))" /></mvt.label>
                        </xsl:when>
                        <xsl:otherwise>
                            <mvt.label><xsl:value-of select="''" /></mvt.label>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>                
            </xsl:choose>
        </ligne>
     </xsl:for-each>
</table-movement>

The problem is that it doesn't work despite the fact that the references do exist in the label file. Thanks a lot for your help.

Part of transaction xml file:

<?xml version="1.0" encoding="UTF-8"?>
<ACCOUNT_EXTRACTION>
    <MOVEMENT MOVEMENT_DESCRIPTION="TENUE COMPTE / FOND GARANTIE R" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="6000,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE FRAIS FIXE AU 31/10/21" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="1134,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="INTERETS DEBITEURS AU 31/10/21" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="149,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE/INTERETS DBT AU 31/10/21" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="28,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="COMM. DE DECOUVERT AU 31/10/21" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="13,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TX/COM. DECOUVERT AU 31/10/21" MOVEMENT_ACCOUNTING_RECORD="AG452032" MOVEMENT_DATE="01/11/2021" ACCOUNTING_DATE="29/10/2021" MOVEMENT_AMOUNT="2,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="VERSEMENT          APPRO COMP" MOVEMENT_ACCOUNTING_RECORD="VE001302150" MOVEMENT_DATE="02/11/2021" ACCOUNTING_DATE="01/11/2021" MOVEMENT_AMOUNT="470000,0000" MOVEMENT_SIDE="C"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="RET DEPLAC" MOVEMENT_ACCOUNTING_RECORD="RA424988" MOVEMENT_DATE="02/11/2021" ACCOUNTING_DATE="03/11/2021" MOVEMENT_AMOUNT="200000,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="RET DAB ON US 130689454025" MOVEMENT_ACCOUNTING_RECORD="RETMO625051" MOVEMENT_DATE="02/11/2021" ACCOUNTING_DATE="03/11/2021" MOVEMENT_AMOUNT="150000,0000" MOVEMENT_SIDE="D">
      <LABELS LABEL_ID="1" LABEL="10000014 - 2021/11/0222:07:34                               "/>
      <LABELS LABEL_ID="2" LABEL="130689454025                                                "/>
    </MOVEMENT>
    <MOVEMENT MOVEMENT_DESCRIPTION="RET DAB ON US 130689501027" MOVEMENT_ACCOUNTING_RECORD="RETMO625339" MOVEMENT_DATE="02/11/2021" ACCOUNTING_DATE="03/11/2021" MOVEMENT_AMOUNT="100000,0000" MOVEMENT_SIDE="D">
      <LABELS LABEL_ID="1" LABEL="10000014 - 2021/11/0222:08:21                               "/>
      <LABELS LABEL_ID="2" LABEL="130689501027                                                "/>
    </MOVEMENT>
    <MOVEMENT MOVEMENT_DESCRIPTION="TENUE COMPTE / FOND GARANTIE R" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="6000,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE FRAIS FIXE AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="1134,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="COMMISSION DE CPTE AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="158,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE COMM. DE CPTE AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="30,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="COMM. DE DECOUVERT AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="21,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="INTERETS DEBITEURS AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="8,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TX/COM. DECOUVERT AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="4,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE/INTERETS DBT AU 30/11/21" MOVEMENT_ACCOUNTING_RECORD="AG526044" MOVEMENT_DATE="01/12/2021" ACCOUNTING_DATE="30/11/2021" MOVEMENT_AMOUNT="2,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TENUE COMPTE / FOND GARANTIE R" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="6000,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE FRAIS FIXE AU 31/12/21" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="1134,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="INTERETS DEBITEURS AU 31/12/21" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="80,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TAXE/INTERETS DBT AU 31/12/21" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="15,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="COMM. DE DECOUVERT AU 31/12/21" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="7,0000" MOVEMENT_SIDE="D"/>
    <MOVEMENT MOVEMENT_DESCRIPTION="TX/COM. DECOUVERT AU 31/12/21" MOVEMENT_ACCOUNTING_RECORD="AG634388" MOVEMENT_DATE="01/01/2022" ACCOUNTING_DATE="31/12/2021" MOVEMENT_AMOUNT="1,0000" MOVEMENT_SIDE="D"/>
</ACCOUNT_EXTRACTION>

Part of label xml file:

<?xml version="1.0" encoding="UTF-8"?>
<transactions-reals-labels>
    <transaction>
        <reference>VE001302150</reference>
        <label>Operation Match label 1</label>
    </transaction>
    <transaction>
        <reference>RETMO625051</reference>
        <label>Operation Match label 2</label>
    </transaction>
    <transaction>
        <reference>RETMO625339</reference>
        <label>Operation Match label 3</label>
    </transaction>
</transactions-reals-labels>

CodePudding user response:

Your expression:

$table-extrait-labels/transaction[reference = @MOVEMENT_ACCOUNTING_RECORD]

is looking for a transaction that has a child reference and a MOVEMENT_ACCOUNTING_RECORDattribute whose values are equal to each other.

If you want to find a transaction whose child reference value is equal to the value of the MOVEMENT_ACCOUNTING_RECORDattribute of the context node (in your case, the currently processed MOVEMENT), you need to use:

$table-extrait-labels/transaction[reference = current()/@MOVEMENT_ACCOUNTING_RECORD]

Or (preferably) construct a key and use it as shown in the examples I pointed to in the comments.


Untested, because no reproducible example was provided.


Note also that testing for the existence of a node and then getting data from that node would be more efficient if you use a variable, instead of repeating the XPath expression.

  • Related