File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Empty element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Empty element" Watch "Empty element" New topic
Author

Empty element

Nils Persson
Greenhorn

Joined: May 25, 2001
Posts: 21
1. How do I check, in my .xsl file, if an element is empty?
2. Why do I keep getting the first element from my .xml file - I don't want it to be in my .html file.
ex:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myXsl.xsl"?>
<DATE>2001-01-01</DATE>
<PERSON>
<NAME><![CDATA[My Name]]>
</NAME>
<AGE>50</AGE>
<PHONE>Yes</PHONE>
</PERSSON>
If I write in my .xsl file:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl utput method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="PERSON">
</xsl:template>
<xsl:template match="PERSON">
<xsl:value-of select="AGE"/>
</xsl:template>
I get:
2001-01-01
50
Why??
Nils
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I'm a bit worried that your example is not valid XML, which could cause the XSL processor to go a bit wierd even if it does manage to parse it. You really need a single "root" element for your tree - currently you have two roots "date" and "person" (and person is not closed correctly, it has an extra "S").

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Nils Persson
Greenhorn

Joined: May 25, 2001
Posts: 21
Ops..my fault I wrote just an example. Once more..
ex:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myXsl.xsl"?>
<PERSONS>
<DATE>2001-01-01</DATE>
<PERSON>
<NAME><![CDATA[My Name]]>
</NAME>
<AGE>50</AGE>
<PHONE>Yes</PHONE>
</PERSON>
<PERSON>
...
<PERSON>
</PERSONS>
If I write in my .xsl file:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl utput method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="PERSON"> NOTE! Not PERSONS!
</xsl:template>
<xsl:template match="PERSON">
<xsl:value-of select="AGE"/>
</xsl:template>
I get:
2001-01-01
50
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
1. How do I check, in my .xsl file, if an element is empty?
XSLT Questions and Answers (url: )http://www.dpawson.co.uk/xsl/sect2/sect21.html) is a priceless source of information. In particular:
�How to test if the value of a certain node is empty?
Depends what you mean by empty.
...
Contains no text content: not(string(.))
Contains no text other than whitespace: not(normalize-space(.))
...�
2. Why do I keep getting the first element from my .xml file - I don't want it to be in my .html file.
Hm. Your XML + XSL do not give any output at all on my computer...
However, when I changed
<xsl:apply-templates select="PERSON">
to
<xsl:apply-templates select="/PERSONS/PERSON">
the output is:
50
I suspect your template for PERSON was not called. (You can check it by inserting some text after <xsl:template>:
<xsl:template match="PERSON">
this is a string to indicate that this template was called
<xsl:value-of select="AGE"/>

...)
Instead, your XSLT processor used so-called �default templates� � special templates which have to be applied when there is no explicit template that matches an element. Default temlate simply outputs an element�s content. Of course, this begs question: why content of the PHONE element is skipped?

[This message has been edited by Mapraputa Is (edited June 11, 2001).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
IF (feel guilty for skipping whitespace handling) AND (have no time to dive into it)
{    post a link to Brett McLaughlin�s �What's the diff?�
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Empty element