How get attribute content by another attribute value with XPath?

倖福魔咒の 提交于 2020-01-07 03:16:58

问题


I have XML like:

<?xml version='1.0' encoding='UTF-8'?>
<ClinicalDocument xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                  xmlns='urn:hl7-org:v3'
                  xmlns:ext='urn:hl7-RU-EHR:v1'
                  xsi:schemaLocation='urn:hl7-org:v3'>
    <author>
        <time value='20160809000000+0300'/>
        <assignedAuthor>
            <id root='1.2.643.5.1.13.3.25.1.1.100.1.1.70' extension='1'/>
            <id root='1.2.643.100.3' extension='03480134121'/>
            <id nullFlavor='NI'/>
        </assignedAuthor>
    </author>
</ClinicalDocument>

I have to get extension in id with root's value = 1.2.643.100.3. I must use XPath 2.0. I have tried:

  1. *[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id' and @id='1.2.643.100.3']/@extension. Not working
  2. /*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id'][2]/@extension, but order of ids can mixed. So that, I should retrieve by id's value

It's needed to me for retrieving value by Java's XPathExpression


回答1:


First, bind namespace prefix, u: to urn:hl7-org:v3.

Then, this XPath,

//u:id[@root='1.2.643.100.3']/@extension

will return 03480134121, as requested.

If you are unable to bind a namespace prefix, you can instead use this XPath,

//*[local-name() ='id' and @root='1.2.643.100.3']/@extension

which will also return 03480134121, as requested.




回答2:


Correct XPath: /*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[local-name()='id' and @root='1.2.643.100.3']/@extension



来源:https://stackoverflow.com/questions/39335844/how-get-attribute-content-by-another-attribute-value-with-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!