命名属性的文本值的 XSLT 条件计数
XSLT Conditional Count of text value of named attribute
我是 xslt 的新手,但我正在努力学习它。我遇到了一些看起来很简单的障碍。我需要对具有特定名称且具有特定值的属性进行计数。我的 XML:
<wd:Bennies>
<wd:BenefitType>Dental</wd:BenefitType>
<wd:multidependents wd:Descriptor="Tom Thumb">
<wd:ID wd:type="WID">1234567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-4</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Buzz LightYear">
<wd:ID wd:type="WID">0987654321</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-3</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Wonder Woman">
<wd:ID wd:type="WID">3214567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-2</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Bill Shakespeare">
<wd:ID wd:type="WID">6543210789</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-1</wd:ID>
</wd:multidependents>
</wd:Bennies>
<wd:Bennies>
<wd:BenefitType>Vision</wd:BenefitType>
<wd:multidependents wd:Descriptor="Tom Thumb">
<wd:ID wd:type="WID">1234567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-4</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Buzz LightYear">
<wd:ID wd:type="WID">0987654321</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-3</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Wonder Woman">
<wd:ID wd:type="WID">3214567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-2</wd:ID>
</wd:multidependents>
</wd:Bennies>
我的代码:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday.report/whatever"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:variable name="DependentsSpouse" select="/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Vision']/wd:multidependents|/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Dental']/wd:multidependents"/>
<xsl:for-each select="$DependentsSpouse">
<xsl:value-of select="wd:ID[@wd:type='Dependent_ID']"/> <xsl:text> </xsl:text>
DEBUG
Value to count <xsl:value-of select="wd:ID[@wd:type = 'Dependent_ID']"/>
</xsl:for-each>
<xsl:variable name="DentalVisionCount"
select="count($DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID'][text() = wd:ID[@wd:type = 'Dependent_ID']] )"/>
count <xsl:value-of select="$DentalVisionCount"/>
</xsl:template>
</xsl:stylesheet>
循环只是为了向我自己证明所有 7 次出现都已加载到变量中。即使在 XML 中出现了 2 次,代码也是 returns 计数为 0。我要的答案是2.
请注意,每个 wd:multidependents 元素中有两个 wd:ID 元素具有 wd:type 属性。我无法控制 XML 模式 - 现在是工作日。
已添加:调试值:
Current 132618-1
Values to count
132618-4|132618-3|132618-2|132618-1|132618-3|132618-2|132618-1|
count 0
谢谢!
你的 count
函数的参数实际上是一个布尔表达式,所以 returns true
或 false
$DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID']/text()='123456-4'
如果你这样做 count(true())
结果是 1。
你要的表情是这样的...
<xsl:variable
name="DentalVisionCount"
select="count($DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID'][text()='123456-4'])"/>
请注意,您可以简化 DependentsSpouse
的设置
<xsl:variable
name="DependentsSpouse"
select="/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Vision' or wd:BenefitType = 'Dental']/wd:multidependents"/>
我是 xslt 的新手,但我正在努力学习它。我遇到了一些看起来很简单的障碍。我需要对具有特定名称且具有特定值的属性进行计数。我的 XML:
<wd:Bennies>
<wd:BenefitType>Dental</wd:BenefitType>
<wd:multidependents wd:Descriptor="Tom Thumb">
<wd:ID wd:type="WID">1234567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-4</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Buzz LightYear">
<wd:ID wd:type="WID">0987654321</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-3</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Wonder Woman">
<wd:ID wd:type="WID">3214567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-2</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Bill Shakespeare">
<wd:ID wd:type="WID">6543210789</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-1</wd:ID>
</wd:multidependents>
</wd:Bennies>
<wd:Bennies>
<wd:BenefitType>Vision</wd:BenefitType>
<wd:multidependents wd:Descriptor="Tom Thumb">
<wd:ID wd:type="WID">1234567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-4</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Buzz LightYear">
<wd:ID wd:type="WID">0987654321</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-3</wd:ID>
</wd:multidependents>
<wd:multidependents wd:Descriptor="Wonder Woman">
<wd:ID wd:type="WID">3214567890</wd:ID>
<wd:ID wd:type="Dependent_ID">123456-2</wd:ID>
</wd:multidependents>
</wd:Bennies>
我的代码:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday.report/whatever"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:variable name="DependentsSpouse" select="/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Vision']/wd:multidependents|/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Dental']/wd:multidependents"/>
<xsl:for-each select="$DependentsSpouse">
<xsl:value-of select="wd:ID[@wd:type='Dependent_ID']"/> <xsl:text> </xsl:text>
DEBUG
Value to count <xsl:value-of select="wd:ID[@wd:type = 'Dependent_ID']"/>
</xsl:for-each>
<xsl:variable name="DentalVisionCount"
select="count($DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID'][text() = wd:ID[@wd:type = 'Dependent_ID']] )"/>
count <xsl:value-of select="$DentalVisionCount"/>
</xsl:template>
</xsl:stylesheet>
循环只是为了向我自己证明所有 7 次出现都已加载到变量中。即使在 XML 中出现了 2 次,代码也是 returns 计数为 0。我要的答案是2.
请注意,每个 wd:multidependents 元素中有两个 wd:ID 元素具有 wd:type 属性。我无法控制 XML 模式 - 现在是工作日。
已添加:调试值:
Current 132618-1
Values to count
132618-4|132618-3|132618-2|132618-1|132618-3|132618-2|132618-1|
count 0
谢谢!
你的 count
函数的参数实际上是一个布尔表达式,所以 returns true
或 false
$DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID']/text()='123456-4'
如果你这样做 count(true())
结果是 1。
你要的表情是这样的...
<xsl:variable
name="DentalVisionCount"
select="count($DependentsSpouse/wd:ID[@wd:type = 'Dependent_ID'][text()='123456-4'])"/>
请注意,您可以简化 DependentsSpouse
<xsl:variable
name="DependentsSpouse"
select="/wd:Report_Data/wd:Bennies[wd:BenefitType = 'Vision' or wd:BenefitType = 'Dental']/wd:multidependents"/>