This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: How to delete empty element tag from output XML?


Thanks to Kay Michael and Jeni Tennison for your help. A general template is
what I am looking for. I wrote a template, it's more like a C function
except more awkward. Most of the codes is for print tag. I wish I can use
<xsl:element> to print the tag which is passed by parameter but don't know
how to assign the parameter to the name of element.

Have a nice day

Guangzu

here's the XSLT stylesheet.

<?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" version="1.0" />
<xsl:template match="/">
	<NameList>
		<My_Favorite>
			<xsl:apply-templates  />	
		</My_Favorite>
	</NameList>
</xsl:template>

<xsl:template match="people">
	<Star>
		<FirstName><xsl:value-of select="N1" /></FirstName>
		<LastName><xsl:value-of select="N2" /></LastName>
		<xsl:call-template name="empty_content">
			<xsl:with-param name="content" select="N3" />
			<xsl:with-param name="tag">MI</xsl:with-param>
			<xsl:with-param name="default">N/A</xsl:with-param>
		</xsl:call-template>
		<xsl:call-template name="empty_content">
			<xsl:with-param name="content" select="Sex" />
			<xsl:with-param name="tag">Gender</xsl:with-param>
			<xsl:with-param name="default"></xsl:with-param>
		</xsl:call-template>
	</Star>
</xsl:template>

<xsl:template name="empty_content">
	<xsl:with-param name="content" />
	<xsl:with-param name="tag" />
	<xsl:with-param name="default" />
	<xsl:if test="$content  !=''">
		<xsl:text>
		</xsl:text>
		<xsl:text
disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="$tag" />
		<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
		<xsl:value-of select="$content" />
		<xsl:text
disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="$tag" />
		<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
		<xsl:text>
		</xsl:text>
	</xsl:if>
	<xsl:if test="$content = ''">
		<xsl:if test="$default !=''">
			<xsl:text>
			</xsl:text>
			<xsl:text
disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="$tag" />
			<xsl:text
disable-output-escaping="yes">&gt;</xsl:text>
			<xsl:value-of select="$default" />
			<xsl:text
disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="$tag" />
			<xsl:text
disable-output-escaping="yes">&gt;</xsl:text>
			<xsl:text>
			</xsl:text>
		</xsl:if>
	</xsl:if>	
</xsl:template>

</xsl:stylesheet>

Input XML
<?xml version="1.0" encoding="UTF-8"?>
<test>
	<people>
		<N1>Tom</N1>
		<N2>Hopkins</N2>
		<N3/>
		<Sex>Male</Sex>
	</people>
	<people>
		<N1>Amy</N1>
		<N2>Smith</N2>
		<N3>R</N3>
		<Sex/>
	</people>
</test>

Output XML (using Xalan)
<?xml version="1.0" encoding="UTF-8"?>
<NameList>
	<My_Favorite>
		<Star>
			<FirstName>Tom</FirstName>
			<LastName>Hopkins</LastName>
			<MI>N/A</MI>
			<Gender>Male</Gender>
		</Star>
		<Star>
			<FirstName>Amy</FirstName>
			<LastName>Smith</LastName>
			<MI>R</MI>
		</Star>
	</My_Favorite>
</NameList>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]