Xpath如何通过索引和属性获取元素 [英] Xpath how to get element by index AND attribute

查看:39
本文介绍了Xpath如何通过索引和属性获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此 xml:

<mets:techMD ID="techMD014">
    <mets:mdWrap MDTYPE="PREMIS:OBJECT">
        <mets:xmlData>
            <premis:object
                    xsi:type="premis:file"
                    xsi:schemaLocation="info:lc/xmlns/premis-v2
                    http://www.loc.gov/standards/premis/v2/premis-v2-0.xsd">
                <premis:objectIdentifier>
                    <premis:objectIdentifierType
                     >filepath</premis:objectIdentifierType>
                    <premis:objectIdentifierValue
                     >bib1234_yyyymmdd_99_x_performance.xml</premis:objectIdentifierValue>
                </premis:objectIdentifier>
            </premis:object>
        </mets:xmlData>
    </mets:mdWrap>
</mets:techMD>
<mets:techMD ID="techMD015">
    <mets:mdWrap MDTYPE="PREMIS:OBJECT">
        <mets:xmlData>
            <premis:object
                    xsi:type="premis:representation"
                    xsi:schemaLocation="info:lc/xmlns/premis-v2
                    http://www.loc.gov/standards/premis/v2/premis-v2-0.xsd">
                <premis:objectIdentifier>
                    <premis:objectIdentifierType
                     >local</premis:objectIdentifierType>
                    <premis:objectIdentifierValue
                     >bib1234_yyyymmdd_99_x</premis:objectIdentifierValue>
                </premis:objectIdentifier>
            </premis:object>
        </mets:xmlData>
    </mets:mdWrap>
</mets:techMD>

我想做一个同时考虑索引和属性的 xpath 查询.即我可以将这两者合并为一个查询吗?(它是我感兴趣的对象"元素周围的东西):

I would like to make a xpath query that takes both index and attribute into account. I.e can I combine these two into ONE query? (Its the stuff around the "object" element Im interested in):

//techMD/mdWrap[
   @MDTYPE=\'PREMIS:OBJECT\'
]/xmlData//object[1]/objectIdentifier/objectIdentifierValue

//techMD/mdWrap[
   @MDTYPE=\'PREMIS:OBJECT\'
]/xmlData//object[
   @xsi:type=\'premis:file\'
]/objectIdentifier/objectIdentifierValue

谢谢!

推荐答案

只需根据部分替换即可:

Just replace according part to:

object[@xsi:type='premis:file'][1]

如果您想要具有给定 xsi:type 值的那些的第一个 object

if you want first object of those who have a given xsi:type value or

object[1][@xsi:type='premis:file']

如果你想要第一个 object,只要它有一个给定的 xsi:type 值.

if you want the first object, providing it has a given xsi:type value.

这篇关于Xpath如何通过索引和属性获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆