how to test an empty element in XSL?

M

Matt

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!!
 
W

William Park

Matt said:
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?
 
R

Richard Tobin

<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
 
M

Matt

How to differentiate between

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

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


Please advise. thanks!!
 
W

William Park

Matt said:
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/
 
J

Joris Gillis

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
<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,
 
D

dan

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"/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top