We help IT Professionals succeed at work.
Get Started

XSLT Exclude Empty Elements

chenegar
chenegar asked
on
393 Views
Last Modified: 2015-07-14
I'm a rank amateur when it comes to XSLT. I have a stylesheet that works except that I need to exclude empty elements. It's a long stylesheet, so here is an abbreviated version.
<?xml version="1.0" encoding="utf-8"?>

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

  <!--
  This is a transform to create an XML file from the ANX file output by the Debt Collection HotDocs intake.
  -->
  
  <xsl:template match="/AnswerSet"> 

	<xsl:element name="OnlineIntake">
.
.
.
                 <xsl:for-each select="//Answer[@name='Factor']/RptValue/MCValue">

			<xsl:variable name="getfactors" select="position()"/>

			<xsl:element name="Factors">			
				<xsl:element name="Factor">					
						<xsl:value-of select="//Answer[@name='Factor']//MCValue[$getfactors]"/>		
				</xsl:element>			
				<xsl:element name="FactorAmt">
					<xsl:value-of select="//Answer[@name='FactorAmount']//NumValue[$getfactors]"/>
				</xsl:element>				
			</xsl:element>  <!-- Factors -->		
		</xsl:for-each> <!-- end of foreach for Factors -->

Open in new window


The input looks like this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnswerSet title="" version="1.1">
	<Answer name="(ANSWER FILE HISTORY)">
		<TextValue>Debt Collection Intake : May 6, 2015, 15:15
Maintenance Interview : February 18, 2015, 15:34
</TextValue>
.
.
.
<Answer name="Factor">
		<RptValue>
			<MCValue>
				<SelValue>Child Care/Work-Related</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Current income taxes</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Mortgage payments</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Rent</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Transportation Expenses/Work-Related</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Tools &amp; Equipment/Work-Related</SelValue>
			</MCValue>
			<MCValue unans="true"/>
		</RptValue>
	</Answer>
<Answer name="Factor">
		<RptValue>
			<MCValue>
				<SelValue>Child Care/Work-Related</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Current income taxes</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Mortgage payments</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Rent</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Transportation Expenses/Work-Related</SelValue>
			</MCValue>
			<MCValue>
				<SelValue>Tools &amp; Equipment/Work-Related</SelValue>
			</MCValue>
			<MCValue unans="true"/>
		</RptValue>
</AnswerSet>

Open in new window

And the output looks like this
<Factors>
    <Factor>Child Care/Work-Related</Factor>
    <FactorAmt>100.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor>Current income taxes</Factor>
    <FactorAmt>250.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor>Mortgage payments</Factor>
    <FactorAmt>600.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor>Rent</Factor>
    <FactorAmt>0.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor>Transportation Expenses/Work-Related</Factor>
    <FactorAmt>100.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor>Tools &amp; Equipment/Work-Related</Factor>
    <FactorAmt>75.0000000</FactorAmt>
  </Factors>
  <Factors>
    <Factor/>
    <FactorAmt/>
  </Factors>

Open in new window

The output should not have the last empty <Factors> elements. Everything I've tried so far either crashes the transform or skips the Factors output. I can't change the input file, so can one of you experts help?
Comment
Watch Question
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE