• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

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
0
scurtis_1
Asked:
scurtis_1
1 Solution
 
aozarovCommented:
Try this:
<xsl:for-each select="../FieldSupportInformation/JobPack/JobPackURL">
<xsl:if test="node() != ''">
              <TemporaryItemDs>
                 <TemporaryItemCode>JBPCK<xsl:value-of select="position()"/></TemporaryItemCode>
                  <TemporaryItemValue>"<xsl:value-of select="." />"</TemporaryItemValue>
              </TemporaryItemDs>
 </xsl:if>
</xsl:for-each>      
0
 
crchambersCommented:
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?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now