Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL - finding if an element exists? 2

Status
Not open for further replies.

tokoh

Programmer
May 23, 2001
13
0
0
AU
Hello,

I am trying to convert some XML to HTML using XSL. One thing I want to be able to do is check to see if an element exists, and if so display its value, and if it does not exist display something like a dash (-).
I know how to check if the element is empty but not if it exists within the document or not!
Does anyone know how this is possible!

Cheers,
Tokoh
 
Hi Tokoh,
count() should do it.

e.g.
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;./missingtag.xsl&quot; ?>
<books>
<book>
<title>book1</title>
<author>manx</author>
</book>
<book>
<title>book2</title>
</book>
</books>

could be transformed with this . . .
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; xmlns:fo=&quot; <xsl:template match=&quot;/&quot;>
<HTML>
<table>
<xsl:apply-templates select=&quot;books&quot;/>
</table>
</HTML>
</xsl:template>

<xsl:template match=&quot;books&quot;>
<xsl:apply-templates select=&quot;book&quot;/>
</xsl:template>

<xsl:template match=&quot;book&quot;>
<tr>
<td><xsl:value-of select=&quot;title&quot;/></td>
<xsl:choose>
<xsl:when test=&quot;count(author)=0&quot;>
<td>-</td>
</xsl:when>
<xsl:eek:therwise>
<td><xsl:value-of select=&quot;author&quot;/></td>
</xsl:eek:therwise>
</xsl:choose>
</tr>
</xsl:template>

</xsl:stylesheet>


Good luck!
 
Manx,

Thanks mate!!
You are a star!!

Tokoh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top