XPath语法 - XSL 1.0

时间:2018-02-07 18:14:32

标签: xpath syntax xslt-1.0 nodes

我尝试使用XSL和XPath语法选择所有元素,其中有多个拾取。我想为每个人返回counter_name。有人可以帮我解释一下语法吗?在此示例中,只有一个带有拾取位置的counter_name,但可能有多个位置存在拾取计数器。

XPATH

 <xsl:value-of select="results/unique_locations/partner_location_ids[count(pickup) &gt; 0]/counter_name" /><br/>

XML

<results>
<unique_locations>
    <counter_name>Lake Buena Vista, FL</counter_name>
    <is_airport>N</is_airport>
    <partner_location_ids>
        <pickup>
        </pickup>
        <dropoff>
            <container>ZR-ORLS001</container>
            <container>ET-ORLR062</container>
            <container>HZ-ORLS011</container>
            <container>HZ-ORLW015</container>
            <container>AV-ORLR004</container>
        </dropoff>
    </partner_location_ids>
    <counter_name>Orlando, FL</counter_name>
    <is_airport>N</is_airport>
    <partner_location_ids>
        <pickup>
            <container>ET-ORLC037</container>
            <container>AV-ORLC021</container>
            <container>ET-ORLC033</container>
            <container>ET-ORLC035</container>
            <container>HZ-ORLS007</container>
            <container>HZ-ORLC004</container>
            <container>HZ-ORLC002</container>
            <container>ZR-ORLS002</container>
            <container>BU-ORLE002</container>
            <container>AV-ORLC019</container>
            <container>ET-ORLR064</container>
            <container>ET-ORLC001</container>
            <container>ET-ORLR063</container>
            <container>ET-ORLR061</container>
            <container>HZ-ORLC011</container>
            <container>HZ-ORLC054</container>
            <container>HZ-ORLN003</container>
            <container>HZ-ORLC007</container>
            <container>HZ-ORLC005</container>
            <container>ZA-ORLC002</container>
            <container>ZA-ORLC003</container>
            <container>ZA-ORLC001</container>
            <container>AV-ORLC002</container>
            <container>AV-ORLC001</container>
            <container>BU-ORLS001</container>
            <container>ET-ORLC012</container>
            <container>AL-ORLR071</container>
            <container>HZ-ORLC022</container>
            <container>ET-ORLC051</container>
            <container>HZ-ORLC025</container>
            <container>HZ-ORLN018</container>
            <container>HZ-ORLC017</container>
            <container>AV-ORLN003</container>
            <container>BU-ORLC002</container>
            <container>BU-ORLC003</container>
            <container>BU-ORLS006</container>
            <container>ET-ORLC027</container>
            <container>ET-ORLC022</container>
            <container>AL-ORLR081</container>
            <container>BU-ORLC005</container>
            <container>HZ-ORLR029</container>
            <container>HZ-ORLC032</container>
            <container>HZ-ORLC031</container>
            <container>HZ-ORLC030</container>
            <container>ET-ORLC021</container>
        </pickup>
        <dropoff>
            <container>HZ-ORLC003</container>
            <container>ZA-ORLC004</container>
            <container>BU-ORLW002</container>
            <container>HZ-ORLC026</container>
            <container>ZR-ORLC010</container>
            <container>AL-ORLR073</container>
        </dropoff>
    </partner_location_ids>
</unique_locations>

1 个答案:

答案 0 :(得分:0)

您的XML结构是不理想的,因为它似乎包含按顺序相互关联的元素,而不是仅包含在同一元素中的包含。但XPath可以解决这个问题。

假设用于评估XPath的上下文节点是您正在检查其内容的<results>的父节点,您似乎想要这些内容:

results/unique_locations/partner_location_ids[pickup/*]/preceding-sibling::counter_name

首先注意谓词:[pickup/*]。如果表达式匹配任何节点,则在布尔上下文中解释的表达式将求值为true。这就是为什么我们需要pickup/*而不仅仅是pickup来区分包含子节点的<pickup>元素和不包含子节点的元素。

此外,请观察使用preceding-sibling轴而不是默认child轴,从每个匹配的<partner_location_ids>步进到相应的(我认为)<counter_name>。< / p>