否则需要 xsl:for-each?

otherwise need xsl:for-each?

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

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

结果:

enter image description here

而我想要的是:

enter image description here

较难的部分: 我怎样才能像这样分离成本组:

enter image description here

谢谢

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

从未达到。所以可能没有必要。