在 XSLT 的数组中存储和读取值



我坚持在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>   

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

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

相关内容

  • 没有找到相关文章

最新更新