【问题标题】:otherwise need xsl:for-each?否则需要 xsl:for-each?
【发布时间】:2021-01-06 06:47:51
【问题描述】:

这是我卡住的 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

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

enter image description here

谢谢

【问题讨论】:

    标签: xml xpath xslt-1.0


    【解决方案1】:

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

    &lt;Cell ss:Index="2" &gt;... 两种情况下。
    现在你应该得到想要的输出了。


    然而,在上面的例子中,

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

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

    【讨论】:

    • 在此答案之后,问题已更新,使其过于宽泛。
    • 谢谢,一切正常。您能否帮助解决“更难的部分:我如何像这样分离 costgrp:”部分?
    • 啊,我明白了,我会把这段代码放在她的新问题中,谢谢
    猜你喜欢
    • 2012-07-20
    • 2016-11-02
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    相关资源
    最近更新 更多