XPath - 如何比较字符串和序列?

时间:2015-11-29 12:40:45

标签: xml xpath compare string-comparison xpath-2.0

简称XML:

...
<team>
    <capitan>Zdeno Chara</capitan>
    <alernativeCapitan>
        <player>Zdeno Chara</player> <!-- NO Chara, he's the capitan obviously -->
        <player>Someone Else</player>
        <player>Someone Other</player>
    </alernativeCapitan>
</team>
<team>
    <capitan>Abc Edf</capitan>
    <alernativeCapitan>
       <player>Abc Xyz</player>
       <player>Bac Edf</player>
       <player>Abc 123</player>
    </alernativeCapitan>
</team>
...

问:如何在XPath中比较一个字符串(capitan)和序列(alternativeCapitan)?

我想知道capitain是否也像其他capitan / player一样。仅在一个团队中进行比较。 (如果是这样,那就错了。这就是全部,如果结果是布尔值或数字则无关紧要。)

感谢。

1 个答案:

答案 0 :(得分:2)

这个问题对我来说不是100%明确,也许你想要这样的事情:

//team[capitan=alernativeCapitan/player]

<强> xpathtester demo

上述XPath将返回team元素中capitan列出alternativeCapitan的所有team。或者,如果您想要相反的情况,则返回capitanalternativeCapitan 不是的所有//team[not(capitan=alernativeCapitan/player)]

<table id="example" class="display table" style="width: 100%; cellspacing: 0;">
    <thead>
        <tr>
            <th>Code</th>
            <th>Name</th>
             <th>Hours</th>
            <th>Class</th>
            <th>Add</th>
         </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Code</th>
            <th>Name</th>
             <th>Hours</th>
            <th>Class</th>
            <th>Add</th>
        </tr>
    </tfoot>
    <tbody>
<?php
$query = "SELECT * FROM class";
$result = mysqli_query($connection,$query) or  die ("Couldn’t execute query.");
while($row = mysqli_fetch_assoc($result))
{
    echo "<tr>                              
        <td>$row[code]</td>
        <td>$row[name]</td>
        <td>$row[hours]</td>";
    $query1 = "SELECT total FROM classtot where code='$row[code]'";
    $result1 = mysqli_query($connection,$query1);
    while ($row=mysqli_fetch_assoc($result1))
    {
        $a=$row['total'];
    }
    $alphabet = range('A','Z');
    $i = 1;
    echo "
    <td><select id='selectError' data-rel='chosen' name='class'>";
    while ($i<=$a)
    {
        $kls=$alphabet[$i-1];
        echo "<option value=$kls> $kls </option>";
        $i=$i+1;
    }
    echo "</select></td>
    <td>                                    
    <a class='btn btn-primary btn-addon m-b-sm btn-xs' href='home_member.php?page=add&id=$row[code]'>
    <i class='fa fa-plus'></i> Add</a>
    </td>
    </tr>";                                 
}

?>
    </tbody>
</table>