xml - 否则需要 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:
谢谢
解决方案
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">
永远达不到。所以可能没有必要。
推荐阅读
- performance - 某些用户的 Microsoft Dynamics CRM 性能缓慢
- azure - 无法更新虚拟机诊断设置
- javascript - 嵌套数组 group-by 与 lodash
- neo4j - Neo4j 管理员负载转储文件给出“系统找不到指定的路径”,无论路径如何
- ios - 如何使用Objective c在iOS中单击通知时打开视图控制器?
- html - 如何使我的幻灯片内容具有响应性?
- javascript - 使用 React Native 创建嵌套 AJAX 请求的专业方法
- php - PHP 正则表达式:如何将 rel=stylesheet 替换为 rel=preload?
- wix - 无法在 UPGRADINGPRODUCTCODE、WIX_UPGRADE_DETECTED 中获取 MSI 属性
- javascript - GraalVM 如何将 java List 对象传输到 javascript,以便我可以在它们上制作 .push() 和 .forEach()?