472,342 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 41891
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Massimiliano Alberti | last post by:
How can I insert an empty element in a list? The insert method has as a parameter the source object to insert... So you have to use it in this way:...
1
by: John | last post by:
Hi, I have an element StaticLayerDataStream referenced in the schema file. This element can be empty or non-empty in the instance file depending...
18
by: Tjerk Wolterink | last post by:
i have the following rule, <xsl:template match="br"> <br/> </xsl:template> This should convert all <br/> to <br/> but, my transformer...
2
by: adrian | last post by:
Hi I'm having the following problem: im my xml file there is an element called <gebdatum>22.09.79</gebdatum> (date of birth in english). this...
7
by: Albert Greinöcker | last post by:
Hi NG, how can I force Xalan (Java) not to create an empty element when there is nothing between begin and end tag, which means that e.g. ...
7
by: kumar.senthil | last post by:
Hi, I'm using XmlSerializer to create an object from the XML string. I would like to know whether I can get a null value for an empty XML element....
1
by: Bill Nguyen | last post by:
below is a very simple XML file. I use ReadXml to read data. When it reaches an empty element as <Product / below, my program crashes. The error:...
5
by: Valery | last post by:
In the following XML: <?xml version="1.0" encoding="utf-8" ?> <Plcy service="ILiability" boId ="LifePolicy, 1"> <Prem service="IPremium" boId...
2
by: jy43 | last post by:
I am writing a method for a web service that returns an empty element.(not null) The SOAP generate the response XML message using shorthand format...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.