hotmeatballsoup
hotmeatballsoup

Reputation: 625

Tricking XSL into interpreting XML element with a particular attribute as being null or empty

Java 8 here, but I don't think that makes any difference as I believe this is a pure XSL question at heart.

I have some code that is producing the following XML (as an example):

<fizz>
  <account>10016</account>
  <accountId>2000001347</accountId>
  <buzz class="null"/>
</fizz>

There are 3 potential scenarios for the buzz element's value at runtime:

So at runtime we might have "Null Class Buzz", "Non-Null Class Buzz" or "Normal Buzz". I do not want to transform the buzz element in the case of "Null Class Buzz". (But I do want to transform Non-Null Class Buzz and Normal Buzz variations.)

Here is my XSL transform:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/fizz">
      <foobar>
        <xsl:copy-of select="account"/>
        <logId><xsl:value-of select="accountId"/></logId>
        <xsl:if test="buzz">
          <FLIMFLAM SEGMENT="1">
            <HAPP>003</HAPP>
            <SADD><xsl:value-of select="buzz"/></SADD>
          </FLIMFLAM>
        </xsl:if>
      <foobar>
    </xsl:template>

</xsl:stylesheet>

The problem is that this transforms Null Class Buzz variations and produces:

<foobar>
  <account>10016</account>
  <logId>2000001347</logId>
  <FLIMFLAM SEGMENT="1">
    <HAPP>003</HAPP>
    <SADD/>
  </FLIMFLAM>
</foobar>

Whereas, in the case of Null Class Buzz, I want buzz ignored entirely:

<foobar>
  <account>10016</account>
  <logId>2000001347</logId>
</foobar>

Any ideas how I can do this? Thanks in advance!

Upvotes: 0

Views: 32

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

As mentioned in the comments:

<xsl:if test="buzz[not(@class='null')]">

will return true when there is at least one buzz element that does not have a class attribute containing the string "null".

Upvotes: 1

Related Questions