Link to home
Start Free TrialLog in
Avatar of scurtis_1
scurtis_1

asked on

XSL: How to ignore empty tags?

Hi,

The following XSL looks for all occurences of the JobPackURL tag and produces a TemporaryItemDS tag for each one. How do I check to see if the JobPackURL tag has any content and if it is empty, don't produce the TemporyItemDS tag?

<?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" indent="yes" />
 
  <xsl:template match="/ | @* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
 
     <xsl:template match="TemporaryItemDs[last()]">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
         <xsl:for-each select="../FieldSupportInformation/JobPack/JobPackURL">
              <TemporaryItemDs>
                 <TemporaryItemCode>JBPCK<xsl:value-of select="position()"/></TemporaryItemCode>
                  <TemporaryItemValue>"<xsl:value-of select="." />"</TemporaryItemValue>
              </TemporaryItemDs>
         </xsl:for-each>      
     </xsl:template>
</xsl:stylesheet>

Thanks
Scott
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crchambers
crchambers

Further  question on this topic:
Must one use a conditional on *every single* node that might possibly be empty? Is there a way to declare from the beginning of the stylesheet that you don't want to output empty tags? It seems verbose and inefficient to have to use an <xsl:if> for every tag... it just seems to add unneccesary length. Any ideas on how to do this more efficiently/elegantly? I have a pretty long list of nodes to process and I dread having to write a conditional for every one. Hopefully, there's a better way?