首页 > 解决方案 > 否则需要 xsl:for-each?

问题描述

这是我卡住的 xslt 的最后一部分,有人建议我错过了什么吗?也许 xsl:otherwise 不需要 xsl:for-each 或者我的循环不正确?我已经尝试从 xsl:otherwise 部分中删除“祖先”,但它有效。我已经在 xsl:otherwise 部分尝试了 xsl:variable(命名为 cost),但它也不起作用。

我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<contracts>
    <ES_CMContract contractName="test_01">
        <productInContracts>
            <ES_CMProductInContract>
                <broadcastRights>
                    <ES_CM2BROADCASTRIGHT>
                        <broadcastRightGroup>
                            <ES_BROADCASTRIGHTGROUP>
                                <costDefinitions>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Distributor"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="FREMANTLE MEDIA"/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Material Cost"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="FREMANTLE MEDIA"/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Dubbing Cost"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="FREMANTLE MEDIA"/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Technical Cost"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="FREMANTLE MEDIA"/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                </costDefinitions>
                            </ES_BROADCASTRIGHTGROUP>
                        </broadcastRightGroup>
                    </ES_CM2BROADCASTRIGHT>
                </broadcastRights>
            </ES_CMProductInContract>
        </productInContracts>
    </ES_CMContract>
    <ES_CMContract contractName="test_02">
        <productInContracts>
            <ES_CMProductInContract>
                <broadcastRights>
                    <ES_CM2BROADCASTRIGHT>
                        <broadcastRightGroup>
                            <ES_BROADCASTRIGHTGROUP>
                                <costDefinitions>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Distributor"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="Walt Disney Co Ltd."/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                    <ES_CM2CostDefinition>
                                        <costGroup>
                                            <ESP_2COSTGROUP name="Dubbing Cost"/>
                                        </costGroup>
                                        <involvedParty>
                                            <ES_FIRM f_name="GRANDVIEW"/>
                                        </involvedParty>
                                    </ES_CM2CostDefinition>
                                </costDefinitions>
                            </ES_BROADCASTRIGHTGROUP>
                        </broadcastRightGroup>
                    </ES_CM2BROADCASTRIGHT>
                </broadcastRights>
            </ES_CMProductInContract>
        </productInContracts>
    </ES_CMContract>
</contracts>

我的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:x="urn:schemas-microsoft-com:office:excel" 
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns:html="http://www.w3.org/TR/REC-html40">
    
    <xsl:template match="contracts">
    
    <xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
        <Styles>
            <Style ss:ID="header" ss:Name="Normal">
                <Font ss:FontName="Verdana" ss:Bold="1"/>
            </Style>
         </Styles>
        
        <Worksheet ss:Name="Report">
            <Table>
                <Row ss:Index="1">
                    <!-- contract name -->
                    <Cell ss:Index="1" ss:StyleID="header">
                        <Data ss:Type="String">contract name</Data>
                    </Cell>
                    <!-- Cost -->
                    <Cell ss:Index="2" ss:StyleID="header">
                        <Data ss:Type="String">Cost</Data>
                    </Cell>
                    <!-- Dubbing Supplier -->
                    <Cell ss:Index="3" ss:StyleID="header">
                        <Data ss:Type="String">Dubbing Supplier</Data>
                    </Cell>
                 </Row>
                
               <xsl:for-each select="ES_CMContract">                
                <xsl:variable name="cost" select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition"/>             
                <xsl:choose>
                    <xsl:when test="productInContracts/ES_CMProductInContract/episodesInContract">
                        <xsl:for-each select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition">  
                               <Row>
                                <Cell ss:Index="1" >
                                    <Data ss:Type="String">
                                        <xsl:value-of select="@contractName"/>
                                    </Data>
                                </Cell>
                                <Cell ss:Index="2" >
                                    <Data ss:Type="String">
                                        <xsl:value-of select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition/costGroup/ESP_2COSTGROUP/@name"/>
                                    </Data>
                                </Cell>                         
                                <Cell ss:Index="3" >
                                    <Data ss:Type="String">
                                        <xsl:value-of select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition/involvedParty/ES_FIRM/@f_name"/>
                                    </Data>
                                </Cell>
                            </Row>
                            </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition">
                        <Row>
                            <Cell ss:Index="1" >
                                <Data ss:Type="String">
                                    <xsl:value-of select="ancestor::ES_CMContract/@contractName"/>
                                </Data>
                            </Cell>
                            <Cell ss:Index="2" >
                                <Data ss:Type="String">
                                    <xsl:value-of select="ancestor::ES_CMContract/productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition/costGroup/ESP_2COSTGROUP/@name"/>
                                </Data>
                            </Cell>
                            <Cell ss:Index="3" >
                                <Data ss:Type="String">
                                    <xsl:value-of select="ancestor::ES_CMContract/productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition/involvedParty/ES_FIRM/@f_name"/>
                                </Data>
                            </Cell>
                        </Row>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </Table>
          </Worksheet>
        </Workbook>
      </xsl:template>
</xsl:stylesheet>

结果:

在此处输入图像描述

我想要什么:

在此处输入图像描述

更难的部分:我怎样才能像这样分离costgrp:

在此处输入图像描述

谢谢

标签: xmlxpathxslt-1.0

解决方案


xsl:for-each作品中相对于它所设置的上下文节点的表达。
所以只需更改

<xsl:value-of select="productInContracts/ES_CMProductInContract/broadcastRights/ES_CM2BROADCASTRIGHT/broadcastRightGroup/ES_BROADCASTRIGHTGROUP/costDefinitions/ES_CM2CostDefinition/costGroup/ESP_2COSTGROUP/@name"/>

<xsl:value-of select="costGroup/ESP_2COSTGROUP/@name"/>

在这两种<Cell ss:Index="2" >...情况下。
现在您应该获得所需的输出。


然而,在上面的例子中,

<xsl:when test="productInContracts/ES_CMProductInContract/episodesInContract">

永远达不到。所以可能没有必要。


推荐阅读