469,270 Members | 1,031 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

how to test an empty element in XSL?

How can XSL detect empty elements, for example, <author></author> or
<author/> ??

The XML structure can be
<book>
<author></author>
<title></title>
</book>

The XSL has the following
<UL>
<LI>book author: <xsl:value-of
select="/book/author"></xsl:value-of></LI>
<LI>book title: <xsl:value-of
select="/book/title"></xsl:value-of></LI>
</UL>

The problem is it will display book author: and book title: even
<author> and <title>
are empty elements.

I tried the following approaches, but still not work.

<xsl:if test="string-length("<xsl:value-of select="author" />") &gt;
0">

<xsl:count(<xsl:value-of select="author" />)></xsl:count>

any ideas? please advise. thanks!!
Jul 20 '05 #1
6 41568
Matt <jr********@hotmail.com> wrote:
How can XSL detect empty elements, for example, <author></author> or
<author/> ??

The XML structure can be
<book>
<author></author>
<title></title>
</book>

The XSL has the following
<UL>
<LI>book author: <xsl:value-of
select="/book/author"></xsl:value-of></LI>
<LI>book title: <xsl:value-of
select="/book/title"></xsl:value-of></LI>
</UL>

The problem is it will display book author: and book title: even
<author> and <title> are empty elements.


Yes. So, what is you're question?

--
William Park <op**********@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #2
In article <ba**************************@posting.google.com >,
Matt <jr********@hotmail.com> wrote:
<xsl:if test="string-length("<xsl:value-of select="author" />") &gt; 0">


You can't (and don't need to) nest XSL elements inside expressions.

Use something like this:

<xsl:if test="string-length(author) &gt; 0">

-- Richard
Jul 20 '05 #3
How to differentiate between

empty element
<author></author>, <author/>

and non-empty elements
<author>Joe</author>
Please advise. thanks!!
Jul 20 '05 #4
Matt <jr********@hotmail.com> wrote:
How to differentiate between

empty element
<author></author>, <author/>

and non-empty elements
<author>Joe</author>


1. Using regex:

<(author)></\1>
<author/>

2. Using Expat XML parser:

start () {
case ${XML_ELEMENT_STACK[1]} in
author) unset author ;;
esac
}
func () {
case ${XML_ELEMENT_STACK[1]} in
author) strcat author "$1" ;;
esac
}
end () {
case ${XML_ELEMENT_STACK[1]} in
author)
if [ "$author" ]; then
echo "author={$author}"
else
echo "author is empty"
fi
esac
}
xml -s start -d func -e end "<author></author>"
xml -s start -d func -e end "<author/>"
xml -s start -d func -e end "<author>Joe</author>"

Ref:
http://freshmeat.net/projects/bashdiff/

--
William Park <op**********@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #5
On 11 Oct 2004 17:09:03 -0700, Matt <jr********@hotmail.com> wrote:
The problem is it will display book author: and book title: even
<author> and <title>
are empty elements.
Hi,

The Xpath expression to check if a node contains text is:
<xsl:if test="//book/author/text()">contains text</xsl:if>

But in this particular case, I would rather use something like this like
this:

<xsl:template match="/">
<ul>
<xsl:apply-templates select="//book/*"/>
</ul>
</xsl:template>

<xsl:template match="book/*[text()]">
<li><xsl:value-of select="local-name()"/>: <xsl:value-of
select="."/></li>
</xsl:template>

<xsl:count(<xsl:value-of select="author" />)></xsl:count>


This is not only illegal xsl (the 'xsl:count' tag doesn't exist) , it is
also syntacticly nonsense.
XSL is an XML language, so an XSL stylesheet must be valid XML, the above
code extract is certainly not.

XSL is really quite easy once you get the hang of it.
regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #6
dan
jr********@hotmail.com (Matt) wrote in message news:<ba**************************@posting.google. com>...
How can XSL detect empty elements, for example, <author></author> or
<author/> ??

The XML structure can be
<book>
<author></author>
<title></title>
</book>

The XSL has the following
<UL>
<LI>book author: <xsl:value-of
select="/book/author"></xsl:value-of></LI>
<LI>book title: <xsl:value-of
select="/book/title"></xsl:value-of></LI>
</UL>

The problem is it will display book author: and book title: even
<author> and <title>
are empty elements.

I tried the following approaches, but still not work.

<xsl:if test="string-length("<xsl:value-of select="author" />") &gt;
0">

<xsl:count(<xsl:value-of select="author" />)></xsl:count>

any ideas? please advise. thanks!!


You could create a template with this predicate.

<xsl:template match="author[ .!='' ]">
<LI>book author: <xsl:value-of select="."/></LI>
</xsl:template>

and then use <xsl:apply-templates select="author"/>
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Massimiliano Alberti | last post: by
1 post views Thread by John | last post: by
18 posts views Thread by Tjerk Wolterink | last post: by
2 posts views Thread by adrian | last post: by
7 posts views Thread by Albert Greinöcker | last post: by
7 posts views Thread by kumar.senthil | last post: by
1 post views Thread by Bill Nguyen | last post: by
5 posts views Thread by Valery | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.