这里是xml
<BenefitDefinitions>
<BenefitDefinition>
<BenefitCode>A</BenefitCode>
<FulfillmentName>Test</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>B</BenefitCode>
<FulfillmentName>Test</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>C</BenefitCode>
<FulfillmentName>Test</FulfillmentName>
</BenefitDefinition>
</BenefitDefinitions>
...
<Risks>
<ContractRisk>
<RiskId>1</RiskId>
<Coverage>
<Cover>
<BenefitCode>A</BenefitCode>
<Cover>
</Coverage>
</ContractRisk>
<ContractRisk>
<RiskId>2</RiskId>
<Coverage>
<Cover>
<BenefitCode>A</BenefitCode>
<Cover>
</Coverage>
</ContractRisk>
</Risks>
我需要对这个应用一个转换,基本上:
For each risk
For each benefit definition group (where a group has the same fulfillmentName)
If the benefit group has a cover associated to it (matched on benefitCode)
display the cover details
Otherwise
display not covered details
坚持使用xslt 1.0,但我不完全理解键的概念以及如何过滤键…所以我知道,如果我没有保险,我可以使用这样的东西:
<xsl:key name="benefits-by-fulfillmentname" match="BenefitDefinition" use="FulfillmentName/text()" />
<xsl:key name="cover-by-benefitcode" match="Cover" use="BenefitCode" />
<!-- TODO: Make BonusSumInsured read from correct extract data when available -->
<xsl:template name="PolicyScheduleBenefits">
<xsl:param name="riskId" select="../../RiskId"/>
<xsl:for-each select="//BenefitDefinition[count(. | key('benefits-by-fulfillmentname', FulfillmentName/text())[1]) = 1]">
<group name="{FulfillmentName}">
<xsl:choose>
<xsl:when test="key('cover-by-benefitcode', key('benefits-by-fulfillmentname', FulfillmentName)/BenefitCode)">
<xsl:text>there is a cover</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>there is not</xsl:text>
</xsl:otherwise>
</xsl:choose>
</group>
但是我不确定如何调整这个来检查每项风险是否存在保险。
我认为问题是您的"按受益码投保"键查找的是所有险的"承保",但您只想检查特定的风险。
要解决这个问题,只需在当前使用密钥后添加一个条件来检查风险,如下所示:
<xsl:when test="key('cover-by-benefitcode', key('benefits-by-fulfillmentname', FulfillmentName)/BenefitCode)
[../../RiskId =$riskId]">
我相信我会这样做:假设这是您的XML输入的代表性示例:
<root>
<BenefitDefinitions>
<BenefitDefinition>
<BenefitCode>A</BenefitCode>
<FulfillmentName>group ABC</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>B</BenefitCode>
<FulfillmentName>group ABC</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>C</BenefitCode>
<FulfillmentName>group ABC</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>B</BenefitCode>
<FulfillmentName>group BD</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>D</BenefitCode>
<FulfillmentName>group BD</FulfillmentName>
</BenefitDefinition>
</BenefitDefinitions>
<Risks>
<ContractRisk>
<RiskId>BC</RiskId>
<Coverage>
<Cover>
<BenefitCode>B</BenefitCode>
</Cover>
<Cover>
<BenefitCode>C</BenefitCode>
</Cover>
</Coverage>
</ContractRisk>
<ContractRisk>
<RiskId>D</RiskId>
<Coverage>
<Cover>
<BenefitCode>D</BenefitCode>
</Cover>
</Coverage>
</ContractRisk>
</Risks>
</root>
那么这个样式表:
XSLT 1.0<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="benefits-by-fulfillmentname" match="BenefitDefinition" use="FulfillmentName" />
<xsl:template match="/">
<risks>
<xsl:for-each select="root/Risks/ContractRisk">
<risk id="{RiskId}">
<xsl:variable name="riskCodes" select="Coverage/Cover/BenefitCode" />
<xsl:for-each select="/root/BenefitDefinitions/BenefitDefinition[count(. | key('benefits-by-fulfillmentname', FulfillmentName)[1]) = 1]">
<group name="{FulfillmentName}">
<xsl:choose>
<xsl:when test="key('benefits-by-fulfillmentname', FulfillmentName)/BenefitCode=$riskCodes">
<xsl:text>there is a cover</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>there is not</xsl:text>
</xsl:otherwise>
</xsl:choose>
</group>
</xsl:for-each>
</risk>
</xsl:for-each>
</risks>
</xsl:template>
</xsl:stylesheet>
将产生以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<risks>
<risk id="BC">
<group name="group ABC">there is a cover</group>
<group name="group BD">there is a cover</group>
</risk>
<risk id="D">
<group name="group ABC">there is not</group>
<group name="group BD">there is a cover</group>
</risk>
</risks>
,
注意:与你之前的问题不同,这不再是一个"存在性测试",而是一个节点集比较。