NickBeaugié
NickBeaugié

Reputation: 730

XSLT Filter comparing two values in same sub node

I need to use XSLT (version 1 unfortunately..) am trying to filter out certain nodes via a comparison of two properties in the sub node.

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  </SOAP-ENV:Header>
  <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <QueriesResponse xmlns="http://schemas.Movies.com/Movies">
      <Films xmlns="http://schemas.Movies.com/Movies">
        <Film>
          <FilmPostings>
            <FilmPosting>
              <FilmPostingDates>
                <FilmPostDate>2017-01-04T19:44:25.9530000-05:00</FilmPostDate>
                <FilmActiveDate>2017-01-04T19:44:25.9530000-05:00</FilmActiveDate>
              </FilmPostingDates>
            </FilmPosting>
          </FilmPostings>
        </Film>
        <Film>
          <FilmPostings>
            <FilmPosting>
              <FilmPostingDates>
                <FilmPostDate>2017-01-04T19:50:06.3830000-05:00</FilmPostDate>
                <FilmActiveDate>2017-01-04T19:50:06.3100000-05:00</FilmActiveDate>
              </FilmPostingDates>
            </FilmPosting>
          </FilmPostings>
        </Film>
        <Film>
          <FilmPostings>
            <FilmPosting>
              <FilmPostingDates>
                <FilmPostDate>2016-12-05T18:03:14.9830000-05:00</FilmPostDate>
                <FilmActiveDate>2017-01-02T00:16:52.7570000-05:00</FilmActiveDate>
              </FilmPostingDates>
            </FilmPosting>
          </FilmPostings>
        </Film>
      </Films>
    </QueriesResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And here is my transform:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://schemas.Movies.com/Movies"
                xmlns:m="http://schemas.Movies.com/Movies"
                exclude-result-prefixes="m"
                >
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <!-- standard identity template -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- This is mean to compare PostDate and ActiveDate, matching if different. It doesn't match any nodes in XML (but should match the final one). -->
  <xsl:template match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmPostDate[1] [.!= m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmActiveDate[1]]]">
  </xsl:template>

  <!-- This code does match the hard coded value (the second node).  -->  
  <!--<xsl:template match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmPostDate[1] [.!= '2017-01-04T19:50:06.3830000-05:00']]">    
  </xsl:template>-->

</xsl:stylesheet>

So, you'll see in the commented-out bit that I can do a match with hard-coded values, so I'm obviously in the right area - I know that the code will exclude a whole Film node if it finds the match. But it's comparing the two node values that doesn't work.

I've tried all kinds of variations on the right side of the comparison, but it doesn't seem able to pick up the value of the ActiveDate.

Upvotes: 0

Views: 526

Answers (1)

Tim C
Tim C

Reputation: 70598

To remove Film elements where FilmPostDate and FilmActiveDate you can actually nest the conditions in the match attribute

<xsl:template 
     match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates[m:FilmPostDate = m:FilmActiveDate]]" />

This assumes only one set of FilmPostDate and FilmActiveDate elements per Film. If there were more than one set, you can try remove Film elements where all occurrences are the same (or rather, then are no occurrences that were different).

<xsl:template 
     match="m:Film[not(m:FilmPostings/m:FilmPosting/m:FilmPostingDates[m:FilmPostDate != m:FilmActiveDate])]" />

Upvotes: 1

Related Questions