xml - XSLT Only Let Through Duplicate Records -


is possible filter on speicif field , when 2 fields same, let 2 hierarchies through. mean:

input data:

<?xml version="1.0" encoding="utf-8"?> <payload>     <set>         <field1>compare</field1>         <field2>info</field2>         <field3>more infor</field3>     </set>     <set>         <field4>compare</field4>         <field5>put through</field5>         <field6>this too</field6>     </set>     <set>         <field1>compare1</field1>         <field2>info</field2>         <field3>more infor</field3>     </set>     <set>         <field4>compare2</field4>         <field5>put through</field5>         <field6>this too</field6>     </set>     <set>         <field1>compare2</field1>         <field2>info</field2>         <field3>more infor</field3>     </set> </payload> 

then compare field1 sind content same "compare" , "compare2" , let 4 through, output looks this:

<?xml version="1.0" encoding="utf-8"?> <payload>     <set>         <field1>compare</field1>         <field2>info</field2>         <field3>more infor</field3>     </set>     <set>         <field4>compare</field4>         <field5>put through</field5>         <field6>this too</field6>     </set>     <set>         <field4>compare2</field4>         <field5>put through</field5>         <field6>this too</field6>     </set>     <set>         <field1>compare2</field1>         <field2>info</field2>         <field3>more infor</field3>     </set> </payload> 

how write xslt compare , let through matching?

do want compare first child element of each set element first child element of other set elements? if understand correctly xslt 1.0 can use key in

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:key name="k1" match="set" use="*[1]"/>  <xsl:strip-space elements="*"/> <xsl:output indent="yes"/>  <xsl:template match="@* | node()">   <xsl:copy>     <xsl:apply-templates select="@* | node()"/>   </xsl:copy> </xsl:template>  <xsl:template match="payload">   <xsl:copy>     <xsl:apply-templates select="set[key('k1', *[1])[2]]"/>   </xsl:copy> </xsl:template>  </xsl:stylesheet> 

with xslt 2.0 can shorter:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:key name="k1" match="set" use="*[1]"/>  <xsl:strip-space elements="*"/> <xsl:output indent="yes"/>  <xsl:template match="@* | node()">   <xsl:copy>     <xsl:apply-templates select="@* | node()"/>   </xsl:copy> </xsl:template>  <xsl:template match="set[not(key('k1', *[1])[2])]"/>  </xsl:stylesheet> 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -