Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Empty element

 
Nils Persson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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").
 
Nils Persson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?�
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic