Filter XML records if the value exists on another xml list

155 Views Asked by At

I have two XMls Main XML and List XML. I am trying to Filter Main XML based on existence of same node value on both XMLs.

Tried the below xslt but is not working as expected.

Xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes" media-type="string"/>

<xsl:param name="cc" select="'list.xml'"/>
<xsl:variable name="list" select="document($cc)/rows/row" />

<xsl:template match="/lines">
<xsl:copy>
    <xsl:apply-templates select="/line[substring-before(/account/seg1, ' ')=$list/cc]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

I am using Cast Iron tool to perform this filter, it supports XSLT 2.0

Main xml:

<lines>
  <line>
        <account>
            <seg1>0101 Expense</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0102 Capital</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0103 Expense</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0104 Expense</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0105 Espense</seg1>
    </account>
 </line>
</lines>

List Xml:

<rows>
 <row>
    <cc>0101</cc>
 </row>
 <row>
    <cc>0103</cc>
 </row>
 <row>
    <cc>0105</cc>
 </row>
 <row>
    <cc>0107</cc>
 </row>
 <row>
    <cc>0109</cc>
 </row>
</rows>

output Xml:

<lines>
 <line>
    <account>
        <seg1>0101</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0103</seg1>
    </account>
 </line>
 <line>
    <account>
        <seg1>0105</seg1>
    </account>
 </line>
</lines>

EDIT1: added xslt i am trying and inputxml with more information.

1

There are 1 best solutions below

3
On

Declare a key <xsl:key name="ref" match="row/cc" use="."/> and then use the identity transformation template plus an empty template

<xsl:template match="line[not(key('ref', account/seg1, doc('list.xml')))]"/>

After the edit, where you substantially changed the input structure, I think the suggested approach should still work if you change the empty template to

<xsl:template match="line[not(key('ref', substring-before(account/seg1, ' '), doc('list.xml')))]"/>