473,543 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:va lue-of select="author" />") &gt;
0">

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

any ideas? please advise. thanks!!
Jul 20 '05 #1
6 42033
Matt <jr********@hot mail.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**********@y ahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #2
In article <ba************ **************@ posting.google. com>,
Matt <jr********@hot mail.com> wrote:
<xsl:if test="string-length("<xsl:va lue-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********@hot mail.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_S TACK[1]} in
author) unset author ;;
esac
}
func () {
case ${XML_ELEMENT_S TACK[1]} in
author) strcat author "$1" ;;
esac
}
end () {
case ${XML_ELEMENT_S TACK[1]} in
author)
if [ "$author" ]; then
echo "author={$autho r}"
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>Jo e</author>"

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

--
William Park <op**********@y ahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #5
On 11 Oct 2004 17:09:03 -0700, Matt <jr********@hot mail.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()">contain s 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:valu e-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********@hotm ail.com (Matt) wrote in message news:<ba******* *************** ****@posting.go ogle.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:va lue-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
2185
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, x (an integer)) Normally a copy constructor is longer to execute than a simple constructor, and this example code uses the constructor once (for...
1
1724
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 name="StaticLayerDataStream" type="xs:hexBinary"> Now in the XML instance file, the parser throws an error when it comes across an empty StaticLayerDataStream...
18
2399
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
4718
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 case it is empty I want to have some text saying that this data is missing.
7
2377
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
34781
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 string) for a string if the corresponding element is missing in the XML. Input XML: <DemoClass><A>some value</A><B></B><DemoClass>
1
3056
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 not set to an instance of an object. How can I assign a value to this empty element? Thanks
5
3763
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 service="ILifeMain" type = "Life1"> <FirstName>Sheila</FirstName> <Age>65</Age> <Relation>spouse</Relation> </L1>
2
2400
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 > < PingURLResponse xmlns=" http://www.multispeak.org/Version_3.0 "> < PingURLResult /> </ PingURLResponse > </ soap:Body >
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7349
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7590
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7735
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.