xslt - Delete section based on search condition -
my requirenament delete relation tag @guid
match 1 of relation's @relationobj
<rolecode tc=32>
or <rolecode tc=31>
or <rolecode tc=8>
only. above logic applicable @relationobj
should match 1 of relation's @relationobj
<rolecode tc=32>
or <rolecode tc=31>
or <rolecode tc=8>
only.
basically want search @guid
, @relationobj
looking other relation tags @relationobj
belongs <rolecode tc=32>
or <rolecode tc=31>
or <rolecode tc=8>
. if condition true delete section.
below xml :
<relations> <relation guid="abcd1234" relationobj="1234"> <rolecode tc="20"/> </relation> <relation guid="xyz123" relationobj="1111"> <rolecode tc="32"/> </relation> <relation guid="def123" relationobj="2222"> <rolecode tc="31"/> </relation> <relation guid="1111" relationobj="2222"> <rolecode tc="98"/> </relation> <relation guid="jkl123" relationobj="3333"> <rolecode tc="8"/> </relation> <relation guid="2222" relationobj="1234"> <rolecode tc="100"/> </relation> </relations>
i tried creating 3 variables extract @relationobj
each <rolecode tc>[32,31 , 8]
inside template. , comparing @objectid
, @relationobj
these 3 variables. problem here , variable getting updated empty value when encounters <relation>
tag while iterating.
from above xml should below xml after transformation.
<relations> <relation guid="abcd1234" relationobj="1234"> <rolecode tc="20"/> </relation> <relation guid="xyz123" relationobj="1111"> <rolecode tc="32"/> </relation> <relation guid="def123" relationobj="2222"> <rolecode tc="31"/> </relation> <relation guid="jkl123" relationobj="3333"> <rolecode tc="8"/> </relation> <relation guid="2222" relationobj="1234"> <rolecode tc="100"/> </relation> </relations>
if observe, <relation>
tag [guid="1111" relationobj="2222"] deleted. because both @guid , @relationobj belongs 1 of rolecodes - 32,31 or 8.
could please let me know best save value in variable without updating new one? or there better approach can achieve this. time.
i approach defining key let extract rolecode
values particular relationobj
, , use key pick relation
elements exclude. if understand requirements correctly think should work:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:key name="rolecodebyrelobj" match="rolecode" use="../@relationobj" /> <!-- copy input output except more specific template applies --> <xsl:template match="@*|node()"> <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy> </xsl:template> <!-- ignore relation guid , relationobj match relationobj of relation rolecode/@tc 8, 31 or 32 (not same matching relation in both cases) --> <xsl:template match="relation[ ( (key('rolecodebyrelobj', @guid)/@tc = '8') or (key('rolecodebyrelobj', @guid)/@tc = '31') or (key('rolecodebyrelobj', @guid)/@tc = '32') ) , ( (key('rolecodebyrelobj', @relationobj)/@tc = '8') or (key('rolecodebyrelobj', @relationobj)/@tc = '31') or (key('rolecodebyrelobj', @relationobj)/@tc = '32') ) ]" /> </xsl:stylesheet>
the way equality tests work in xpath when 1 side node set test true if any of nodes in set match value. for
key('rolecodebyrelobj', @guid)/@tc = '31'
when guid
"2222" 2 rolecode
elements key function (tc=31
, tc=98
), , overall test succeeds because 1 of them matches target value.
Comments
Post a Comment