user1495523
user1495523

Reputation: 505

xslt: Filtering xml data based on negative list

Could you please let me know in case it is possible to filter data based on a negative list?
ie. I dont want records matching a particular string or less than certain value. Any help would be highly appriciated

Sample input xml

<?xml version="1.0" encoding="UTF-8"?>
<top>
    <Results>
        <a>no</a>
        <b>10</b>
        <c>12</c>
        <d>9</d>
    </Results>
    <Results>
        <a>_no_</a>
        <b>8</b>
        <c>50</c>
        <d>12</d>
    </Results>
    <Results>
        <a>yes</a>
        <b>6</b>
        <c>55</c>
        <d>56</d>
    </Results>
    <Results>
        <a>yes</a>
        <b>23</b>
        <c>32</c>
        <d>34</d>
    </Results>
</top>

In this I want to filter out
Not (a ~ 'no') and (b > 8)
The expected output is

<?xml version="1.0" encoding="UTF-8"?>
<top>
    <Results>
        <a>yes</a>
        <b>23</b>
        <c>32</c>
        <d>34</d>
    </Results>
</top>

Upvotes: 0

Views: 90

Answers (1)

Fernando
Fernando

Reputation: 1278

You can try with this xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

  <!-- Identity transform: copy everything from input tree to output tree -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Template for those elements we want to filter. It is an empty template so it does nothing: -->
  <xsl:template match="Results[not ((a != 'no') and (b &gt; 8))]" />
</xsl:stylesheet>

I hope it helps.

Upvotes: 1

Related Questions