Xpath 1.0:无法获取特定元素的值。返回重复的值

时间:2017-07-12 04:02:09

标签: xpath

我有以下XML:

<Resource>
    <ResourceSummaryBag>
        <Entry>
            <ObjectName>NR_LDI</ObjectName>
            <usage_prm>
                <Entry>
                    <UsedEntries>98416</UsedEntries>
                </Entry>         
            </usage_prm>
        </Entry>
        <Entry>
            <ObjectName>R_LDI</ObjectName>
            <usage_prm>
                <Entry>
                    <UsedEntries>13265</UsedEntries>
                </Entry>
            </usage_prm>
        </Entry>
    </ResourceSummaryBag>
</Resource>

我想通过 ObjectName 元素提取 usage_prm / UsedEntries 值。 如果我使用包含,我会得到&#39; R_LDI &#39;的重复值对象,但我想要特定 R_LDI NR_LDI 对象的值。

Resource/ResourceSummaryBag/Entry[./ObjectName[contains(.,'R_LDI')]]/usage_prm/Entry/UsedEntries 

结果:98416 13265

任何解决方案? 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <div class="form-group"> <div class="col-md-6"> <select name="category" id="e2" class="form-control"> <?php $conn = mysqli_connect("localhost","user","pass","db"); $query = $conn->query("SELECT * FROM t_category"); while ($dataCategory = $query->fetch_assoc()){ ?> <option value="<?php echo $dataCategory['category']; ?>"> <?php echo $dataCategory['category']; ?> </option> <?php } ?> </select> <script> $('#e2').change(function(){ var opt = $("#e2 option:selected" ).val(); console.log(opt); //output console log }); </script> </div> + =代替normalize-space()

contains()

./ObjectName[normalize-space()='R_LDI'] 从字符串中去除前导和尾随空格,用单个空格替换空白字符序列,并返回结果字符串 [MDN ]