Victor
Victor

Reputation: 333

Storing and reading value in Array in XSLT

I m stuck with using of array in XSLT1.0. I have never used array in XSLT. There is a requirement in which I need to store the value in Array and use it later. Infact we need to use the array position

The Input XML is

<document>
<party>
    <gtin>1000909090</gtin>
    <pos>
        <attrGroupMany name="temperatureInformation">
            <row>
                <attr name="temperatureCode">STORAGE</attr>
                <attrQualMany name="temperature">
                    <value qual="FAH">10</value>
                </attrQualMany>
                <attrGroupMany name="temperatureStats">
                    <row>
                        <attr name="StatsCode">CODE1</attr>
                    </row>
                    <row>
                        <attr name="StatsCode">CODE2</attr>
                    </row>
                </attrGroupMany>
            </row>
            <row>
                <attr name="temperatureCode">HANDLING</attr>
                <attrQualMany name="temperature">
                    <value qual="FAH">30</value>
                </attrQualMany>
                <attrGroupMany name="temperatureStats">
                    <row>
                        <attr name="StatsCode">CODE3</attr>
                    </row>
                    <row>
                        <attr name="StatsCode">CODE4</attr>
                    </row>
                </attrGroupMany>
            </row>
        </attrGroupMany>
    </pos>
</party>
</document>

Expected Output is

<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
    <Relationship>
        <RelationType>temperatureInformation_details</RelationType>
        <RelatedItems>
            <RelatedItem referenceKey="temperatureInformation_details-STORAGE-temperature-FAH-10-1" />
            <RelatedItem referenceKey="temperatureInformation_details-HANDLING-temperature-FAH-30-2" />
        </RelatedItems>
    </Relationship>
    <Relationship>
        <RelationType>temperatureStats</RelationType>
        <RelatedItems>
            <RelatedItem referenceKey="temperatureStats-CODE1-1-1">
                <Attribute name="temperatureInformationreferenceKey">temperatureInformation_details-STORAGE-temperature-FAH-10-1"</Attribute>
            </RelatedItem>
            <RelatedItem referenceKey="temperatureStats-CODE2-2-1">
                <Attribute name="temperatureInformationreferenceKey">temperatureInformation_details-STORAGE-temperature-FAH-10-1"</Attribute>
            </RelatedItem>
            <RelatedItem referenceKey="temperatureStats-CODE3-1-2">
                <Attribute name="temperatureInformationreferenceKey">temperatureInformation_details-HANDLING-temperature-FAH-30-2</Attribute>
            </RelatedItem>
            <RelatedItem referenceKey="temperatureStats-CODE4-2-2">
                <Attribute name="temperatureInformationreferenceKey">temperatureInformation_details-HANDLING-temperature-FAH-30-2</Attribute>
            </RelatedItem>
        </RelatedItems>
    </Relationship>
</RelationshipData>
</CatalogItem>

I am using the below XSLT. I have written code except the code to store value in array in first one and retrieve from array in second one.

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>



<xsl:template match="document"> 
    <CatalogItem>
        <RelationshipData>
            <Relationship>
                <RelationType>temperatureInformation_details</RelationType>  
                <RelatedItems>      
                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">                        
                        <RelatedItem>
                            <xsl:attribute name="referenceKey">
                                <xsl:value-of select="concat('temperatureInformation_details','-',attr[@name='temperatureCode'],'-',attrQualMany/@name,'-',attrQualMany/value/@qual,'-',attrQualMany/value,'-', position()    )"/>
                            </xsl:attribute>                                                            
                        </RelatedItem>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>

            <Relationship>
                <RelationType>temperatureStats</RelationType>  
                <RelatedItems>    
                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">    
                        <xsl:variable name="v_temperatureInformation_position" select="position()"/>                            
                        <xsl:for-each select="attrGroupMany[@name ='temperatureStats']/row">    

                            <RelatedItem>
                                <xsl:attribute name="referenceKey">
                                    <xsl:value-of select="concat('temperatureStats','-',attr[@name='StatsCode'],'-', position(),'-', $v_temperatureInformation_position    )"/>
                                </xsl:attribute>    

                                <Attribute name="temperatureInformationreferenceKey">

                                    <xsl:value-of select="'Dummy'"/>
                                    <!-- Not sure of XSLT code here but the pseudo code does like this

                                If v_temperatureInformation_position = 1
                                Select
                                The first value of reference key of temperatureInformation_details stored in array 
                                If v_temperatureInformation_position = 2
                                Select
                                The second value of reference key of temperatureInformation_details stored in array                                     
                                If v_temperatureInformation_position = 3
                                Select
                                The third value of reference key of temperatureInformation_details stored in array 

                                And so on.. -->

                                </Attribute>

                            </RelatedItem>
                        </xsl:for-each>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>

        </RelationshipData>
    </CatalogItem>

</xsl:template> 

</xsl:stylesheet>   

Upvotes: 0

Views: 2643

Answers (1)

Tim C
Tim C

Reputation: 70638

You could create a named template to output the value you were trying to "store"

<xsl:template name="ref">
    <xsl:value-of select="concat('temperatureInformation_details','-',attr[@name='temperatureCode'],'-',attrQualMany/@name,'-',attrQualMany/value/@qual,'-',attrQualMany/value,'-', position()    )"/>
</xsl:template>

Then in your second xsl:for-each you could store this in a variable for use in the inner loop

<xsl:variable name="data">
   <xsl:call-template name="ref" />
</xsl:variable>

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="document"> 
    <CatalogItem>
        <RelationshipData>
            <Relationship>
                <RelationType>temperatureInformation_details</RelationType>  
                <RelatedItems>      
                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">                        
                        <RelatedItem>
                            <xsl:attribute name="referenceKey">
                                <xsl:call-template name="ref" />
                            </xsl:attribute>                                                            
                        </RelatedItem>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>
            <Relationship>
                <RelationType>temperatureStats</RelationType>  
                <RelatedItems>    
                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">    
                        <xsl:variable name="v_temperatureInformation_position" select="position()"/>   
                        <xsl:variable name="data">
                            <xsl:call-template name="ref" />
                        </xsl:variable>
                        <xsl:for-each select="attrGroupMany[@name ='temperatureStats']/row">    
                            <RelatedItem>
                                <xsl:attribute name="referenceKey">
                                    <xsl:value-of select="concat('temperatureStats','-',attr[@name='StatsCode'],'-', position(),'-', $v_temperatureInformation_position    )"/>
                                </xsl:attribute>    
                                <Attribute name="temperatureInformationreferenceKey">
                                    <xsl:value-of select="$data" />
                                </Attribute>
                            </RelatedItem>
                        </xsl:for-each>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>
        </RelationshipData>
    </CatalogItem>
</xsl:template> 

<xsl:template name="ref">
    <xsl:value-of select="concat('temperatureInformation_details','-',attr[@name='temperatureCode'],'-',attrQualMany/@name,'-',attrQualMany/value/@qual,'-',attrQualMany/value,'-', position()    )"/>
</xsl:template>
</xsl:stylesheet>   

Upvotes: 1

Related Questions