How get attribute content by another attribute val

2019-09-15 04:53发布

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

2条回答
做自己的国王
2楼-- · 2019-09-15 05:22

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

查看更多
祖国的老花朵
3楼-- · 2019-09-15 05:37

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.

查看更多
登录 后发表回答