问题
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:
*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id' and @id='1.2.643.100.3']/@extension
. Not working/*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id'][2]/@extension
, but order ofid
s can mixed. So that, I should retrieve byid
'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