如何根据子

时间:2015-07-22 12:02:46

标签: xml xslt xpath

我需要得到"出生日期"节点值仅在属性" Benefit Type ID"等于" BEN13" 属性"依赖于利益"等于" Y"。

XML

<component>
<attributes name="FlexDependants">
    <attribute name="DataInstance">11</attribute>
    <attribute name="RowNumber">1</attribute>
    <attribute name="Date of Birth">Nov 11 1978</attribute>
    <component name="Allocation">
        <attributes name="Allocation">
            <attribute name="DataInstance">24</attribute>
            <attribute name="Benefit Type ID">BEN13</attribute>
            <attribute name="Dependant of Benefit">Y</attribute>
        </attributes>
    </component>
</attributes>
<attributes name="FlexDependants">
    <attribute name="DataInstance">10</attribute>
    <attribute name="RowNumber">2</attribute>
    <attribute name="Date of Birth">Oct 12 1984</attribute>
    <component name="Allocation">
        <attributes name="Allocation">
            <attribute name="DataInstance">23</attribute>
            <attribute name="Benefit Type ID">BEN13</attribute>
            <attribute name="Dependant of Benefit">N</attribute>
        </attributes>
    </component>
</attributes>

所以基本上我需要在一个声明中有两个谓词,但我不知道该怎么做..

我试过了:

/component/attributes/component/attributes/
attribute[@name='Benefit Type ID' and text()='BEN13'][@name='Dependant of Benefit' and text()='Y']/
../../../attribute[@name='Date of Birth']

^但这不起作用。

问题在于将两个谓词连接在一起......它们可以单独工作但不能一起工作。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您可以尝试这种方式(格式化以便于阅读):

/component
/attributes[
    component
    /attributes[
        attribute[@name='Benefit Type ID' and .='BEN13'] 
            and 
        attribute[@name='Dependant of Benefit' and .='Y']
    ]
]/attribute[@name='Date of Birth']

或使用较少嵌套谓词的这个可能更容易阅读:

/component
/attributes[
    component/attributes/attribute[@name='Benefit Type ID' and .='BEN13'] 
        and 
    component/attributes/attribute[@name='Dependant of Benefit' and .='Y']
]/attribute[@name='Date of Birth']

答案 1 :(得分:0)

这是两个不同的属性,因此您不能同时指定两个条件:

 /component/attributes[attribute[@name='Date of Birth']]
                /component/attributes[attribute[@name='Benefit Type ID' and .='BEN13']]
                                     [attribute[@name='Dependant of Benefit' and .='Y']]