XSLT条件计数命名属性的文本值

时间:2015-07-28 20:37:21

标签: xslt count conditional

我对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次出现,但代码返回的计数为0。我想要的答案是2。

请注意,每个wd:multidependents元素中有两个wd:ID元素,它们具有wd:type属性。我无法控制XML模式 - 它是Workday。

ADDED:调试值:

    Current 132618-1


        Values to count
        132618-4|132618-3|132618-2|132618-1|132618-3|132618-2|132618-1|
           count 0

谢谢!

1 个答案:

答案 0 :(得分:0)

count函数的参数实际上是一个布尔表达式,因此返回truefalse

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