在XSLT中存储和读取Array中的值

时间:2016-07-08 08:36:24

标签: xml xslt xslt-1.0 xslt-grouping

我坚持在XSLT1.0中使用数组。我从未在XSLT中使用过数组。我需要将值存储在Array中并在以后使用它。事实上,我们需要使用数组位置

输入XML

<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>

预期输出

<?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>

我正在使用下面的XSLT。我编写了代码,除了代码在第一个数组中存储值,在第二个数组中从数组中检索。

<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>   

1 个答案:

答案 0 :(得分:1)

您可以创建一个命名模板来输出您尝试“存储”的值

<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:for-each中,你可以将它存储在一个变量中,以便在内循环中使用

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

试试这个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>