挣扎于 xslt 分组和查询 xpath 计数 [英] Struggling with xslt grouping and querying against xpath count
问题描述
所以我坚持使用 XSLT 1.0
So I'm stuck with XSLT 1.0
在我的 XML 中,我有这样的东西:
In my XML I have something like this:
<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>
...
<Coverage>
<Cover>
<BenefitCode>A</BenefitCode>
<Cover>
</Coverage>
逻辑需要是有多个具有相同fulfillmentName 的好处定义的地方——我需要检查它们中是否有任何一个作为封面存在.两者之间的联系是BenefitCode.
The logic needs to be where there are multiple benefit Definitions that have that same fulfillmentName - I need to check if any of them exist as a cover. With the linkage between the two being the BenefitCode.
我尝试使用密钥根据履行名称将福利定义分组
I tried using a key to get the benefitDefinitions into groups based on the fulfillment name
<xsl:key name="benefits-by-fulfillmentname" match="BenefitDefinition" use="FulfillmentName/text()" />
我可以遍历超过 1 个的组
and I can iterate through the groups where there is more than 1
<xsl:for-each select="//BenefitDefinition[count(. | key('benefits-by-fulfillmentname', FulfillmentName/text())[1]) = 1]">
我还可以通过以下方式遍历该组的 BenefitDefinitions:
I could also iterate through the benefitDefinitions of that group by:
<xsl:for-each select="key('benefits-by-fulfillmentname', FulfillmentName/text())">
但我想不通的是如何使用密钥来计数.
But what I can't figure out is how I can make use of the key to get a count.
我想要这样的东西:
<xsl:value-of select="count(../../../../Contract/Risks/PersonContractRisk[RiskId = $riskId]/Coverage/Cover[BenefitCode=key('benefits-by-fulfillmentname', BenefitCode)])"/>
但这不起作用 - 我对 xslt 不熟练,所以并不奇怪.
But that's not working - I'm not skilled with xslt so not too surprising.
我所有的搜索只是告诉我如何计算属于该组的福利定义..但我需要更深入地了解该福利定义组存在多少个保险.
All my searching just tells me how to count the benefitDefinitions that belong in that group.. but I need to go more advanced and find out how many covers exist for that benefitDefinition group.
几乎我想去的地方(如果这有帮助)是针对每个 BenefitDefinition 组,如果有封面输出 1 件事,否则输出其他内容.
Pretty much where I want to go to (in case this helps) is for each benefitDefinition group if there is a cover output 1 thing, otherwise output something else.
推荐答案
你不需要计数或迭代;一个简单的存在测试就足够了.
You don't need to count or iterate; a simple existence test will suffice.
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:key name="cover-by-benefitcode" match="Cover" use="BenefitCode" />
<xsl:template match="/">
<output>
<xsl:for-each select="root/BenefitDefinitions/BenefitDefinition[count(. | key('benefits-by-fulfillmentname', FulfillmentName)[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:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
当这应用于以下有效 XML 测试输入时:
When this is applied to the following valid XML test input:
<root>
<BenefitDefinitions>
<BenefitDefinition>
<BenefitCode>A</BenefitCode>
<FulfillmentName>group A</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>B</BenefitCode>
<FulfillmentName>group A</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>C</BenefitCode>
<FulfillmentName>group A</FulfillmentName>
</BenefitDefinition>
<BenefitDefinition>
<BenefitCode>C</BenefitCode>
<FulfillmentName>group B</FulfillmentName>
</BenefitDefinition>
</BenefitDefinitions>
<Coverage>
<Cover>
<BenefitCode>B</BenefitCode>
</Cover>
</Coverage>
</root>
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<group name="group A">there is a cover</group>
<group name="group B">there is not</group>
</output>
这篇关于挣扎于 xslt 分组和查询 xpath 计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!