Filter XPath list based on the values ​​in the corresponding list

Given this XML

<DiagList> <Diag id="1" icd="400"/> <Diag id="2" icd="401"/> <Diag id="3" icd="402"/> <Diag id="4" icd="400"/> </DiagList> <ICDList> <ICD id="400" description="First one"/> <ICD id="401" description="Second one"/> <icd id="402" description="Third one"/> <ICDList> 

I want to write an Xpath request that selects Diags, which contains the corresponding ICD description. some text.

So, for example, if I specified the text "st", then I should get Diags "1" and "4". If I specify the text "ir", I should get the diags "1", "3" and "4".

I tried

 /DiagList/Diag[contains(lcase(/ICDList/ICD[@id=/DiagList/Diag/@icd]/@description), 'st')] 

and variations.

I was not able to get it to work. He doesn't seem to like the @icd nested link.

Is it possible?

thanks ben

+6
source share
2 answers

Using

  /*/DiagList /* [@icd = /*/ICDList/* [contains(@description, $pText)] /@id ] 

where $pText should be replaced with the required string literal.

XSLT Based Validation :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my "> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:param name="pText" select="'ir'"/> <xsl:template match="/"> <xsl:copy-of select= "/*/DiagList /* [@icd = /*/ICDList/* [contains(@description, $pText)] /@id ]"/> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the provided XML document (corrected for the correct form):

 <t> <DiagList> <Diag id="1" icd="400"/> <Diag id="2" icd="401"/> <Diag id="3" icd="402"/> <Diag id="4" icd="400"/> </DiagList> <ICDList> <ICD id="400" description="First one"/> <ICD id="401" description="Second one"/> <icd id="402" description="Third one"/> </ICDList> </t> 

selected nodes are selected and copied to the output :

 <Diag id="1" icd="400" /> <Diag id="3" icd="402" /> <Diag id="4" icd="400" /> 

when we set in the above conversion :

  <xsl:param name="pText" select="'st'"/> 

, then again the desired result is obtained:

 <Diag id="1" icd="400" /> <Diag id="4" icd="400" /> 
+2
source

What you are missing is the concept of current() . This function refers to the node context for the entire xpath expression, unlike . which refers to the current node in the xpath expression.

So you can use:

 /DiagList/Diag[contains(lcase(/ICDList/ICD[@id=current()/@icd]/@description), 'st')] 
-1
source

Source: https://habr.com/ru/post/901257/


All Articles