This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: How to delete empty element tag from output XML?
- To: xsl-list at mulberrytech dot com
- Subject: RE: How to delete empty element tag from output XML?
- From: "Guangzu Wang (Houston)" <Guangzu dot Wang at channelinx dot com>
- Date: Thu, 26 Oct 2000 13:21:32 -0400
- Reply-To: xsl-list at mulberrytech dot com
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"><</xsl:text><xsl:value-of select="$tag" />
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="$content" />
<xsl:text
disable-output-escaping="yes"></</xsl:text><xsl:value-of select="$tag" />
<xsl:text disable-output-escaping="yes">></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"><</xsl:text><xsl:value-of select="$tag" />
<xsl:text
disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="$default" />
<xsl:text
disable-output-escaping="yes"></</xsl:text><xsl:value-of select="$tag" />
<xsl:text
disable-output-escaping="yes">></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