Link to home
Start Free TrialLog in
Avatar of chenegar
chenegar

asked on

XSLT: Prevent empty tags

I'd like to prevent empty tags from being created in my xml output. The input looks like this:
<Answer name="HouseholdDemographics">
		<RptValue>
			<TextValue unans="true"/>
			<TextValue unans="true"/>
			<TextValue unans="true"/>
			<TextValue>Disability within Household</TextValue>
		</RptValue>
</Answer>

Open in new window


For output, I need this:
<DemographicList>    
    <Demographic>Disability within Household</Demographic>
</DemographicList>

Open in new window

but I'm getting this:
<DemographicList>
    <Demographic/>
    <Demographic/>
    <Demographic/>
    <Demographic>Disability within Household</Demographic>
</DemographicList>

Open in new window

My transform code is currently this:
<xsl:element name="DemographicList">
	<xsl:apply-templates select="//Answer[@name='HouseholdDemographics']/RptValue/TextValue"></xsl:apply-templates>
</xsl:element>

Open in new window

and
<xsl:template match="//Answer[@name='HouseholdDemographics']/RptValue/TextValue">

	<xsl:variable name="getdemogs" select="position()"/>
	<xsl:variable name="householddemog" select="//Answer[@name='HouseholdDemographics']//RptValue/TextValue[$getdemogs]"/>	
	
	<xsl:if test="normalize-space() or normalize-space($getdemogs)">		
			<xsl:element name="Demographic">
				<xsl:value-of select="$householddemog"/>
			</xsl:element>
					
	</xsl:if> 
	
</xsl:template>

Open in new window


How can I skip those empty tags in my xml output?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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 chenegar
chenegar

ASKER

Exactly what I needed! Thank you.