[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: RE: How to delete empty element tag from output XML?
From: "Guangzu Wang (Houston)" <Guangzu.Wang@xxxxxxxxxxxxxx>
Date: Thu, 26 Oct 2000 13:21:32 -0400
remove xml version tag
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.