473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 42023
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: (where MyObject is an object with only a member,...
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 on circumstances. <xs:element...
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 transforms it all to
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 might be full (like in the example) or empty. in...
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. <myTag></myTag> ....does not become... <myTag/>
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. Actually the XmlSerializer assigns "" (empty...
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: System.NullReferenceException: Object reference...
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 ="RegularPremium, 1"></Prem> <L1...
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 for the empty return object. < soap:Body > ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.