473,782 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defaulting empty XML elements

Hi,

I have an XML file which I want to present as a HTML table, and I'm
looking for a quick way of defaulting all empty elements in my XML file
to a nbsp (using the   notation) in my XSL transformation file, so
that if I have for example an XML record of

<record>
<elem1>fii</elem1>
<elem2 />
<elem3 />
<elem4>foo</elem4>
</record>

this would get represented with my XSL transformation as

<table>
<tr>
<td>elem1</td>
<td>elem2</td>
<td>elem3</td>
<td>elem4</td>
</tr>
<tr>
<td>fii</td>
<td> </td>
<td> </td>
<td>foo</td>
</tr>
</table>

It seems a bit tedious to use the structure

<td>
<xsl:choose>
<xsl:when test="string-length(elem1) > 0">
<xsl:value-of select="elem1"/>
</xsl:when>
<xsl:otherwis e>
 
</xsl:otherwise>
</xsl:choose>
</td>

for all of my XML elements in my XSL file...also, I don't want to do the
defaulting on XML side (with a DTD), since I might use the same XML data
for different purposes than just HTML tables, in which case the default
values I want might be something else than the nbsp. That is why I would
rather keep this nbsp defaulting on XSL side, so that this particular
defaulting would be related to this HTML table output format only.

BR,
Jyrki Keisala
Jul 20 '05 #1
4 4042
On Thu, 1 Jan 2004 10:24:58 +0000 (UTC), Jyrki Keisala
<wh*****@wherev er.com> wrote:
It seems a bit tedious to use the structure


XSLT _is_ tedious. Get used to it, or look at ways of automatically
creating it.
--
Klein bottle for rent. Apply within.
Jul 20 '05 #2

"Jyrki Keisala" <wh*****@wherev er.com> wrote in message
news:Xn******** *************** *********@212.5 0.131.130...
this would get represented with my XSL transformation as

<table>
<tr>
<td>elem1</td>
<td>elem2</td>
<td>elem3</td>
<td>elem4</td>
</tr>
<tr>
<td>fii</td>
<td> </td>
<td> </td>
<td>foo</td>
</tr>
</table>

It seems a bit tedious to use the structure

<td>
<xsl:choose>
<xsl:when test="string-length(elem1) > 0">
<xsl:value-of select="elem1"/>
</xsl:when>
<xsl:otherwis e>
 
</xsl:otherwise>
</xsl:choose>
</td>


Hi,

I agree with the other poster : XSL can get tedious. The standard
technique in programming to allow reuse (instead of code duplication) is of
course, abstraction. So you could take the code snippets you wrote and work
them into a template that is invoked as needed. For example : assuming that
<XPathA> is an XPath expression that selects all the nodes that would be
output into your table, you could write :

<xsl:template name="drawTable ">
<xsl:param name="nodes" />
<xsl:param name="defaultVa lue" />
<xsl:for-each select="$nodes" >
<td>
<xsl:choose>
<xsl:when test="string-length(elem1) > 0">
<xsl:value-of select="elem1"/>
</xsl:when>
<xsl:otherwise> <xsl:value-of select="$defaul tValue"
/></xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>
</xsl:template>

...and invoke this by saying :

<xsl:call-template name="drawTable ">
<xsl:with-param name="nodes" select="<XPathA >" />
<xsl:with-param name="defaultVa lue" select=" " />
</xsl:call-template>

This abstraction may or may not work for you. The abstraction needs to
be tuned to your application and your programming style.

Regards,
Kenneth
Jul 20 '05 #3
> Hi,

I agree with the other poster : XSL can get tedious. The standard
technique in programming to allow reuse (instead of code duplication)
is of course, abstraction. So you could take the code snippets you
wrote and work them into a template that is invoked as needed. For
example : assuming that <XPathA> is an XPath expression that selects
all the nodes that would be output into your table, you could write :

<xsl:template name="drawTable ">
<xsl:param name="nodes" />
<xsl:param name="defaultVa lue" />
<xsl:for-each select="$nodes" >
<td>
<xsl:choose>
<xsl:when test="string-length(elem1) > 0">
<xsl:value-of select="elem1"/>
</xsl:when>
<xsl:otherwise> <xsl:value-of select="$defaul tValue"
/></xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>
</xsl:template>

...and invoke this by saying :

<xsl:call-template name="drawTable ">
<xsl:with-param name="nodes" select="<XPathA >" />
<xsl:with-param name="defaultVa lue" select=" " />
</xsl:call-template>

This abstraction may or may not work for you. The abstraction
needs to
be tuned to your application and your programming style.

Regards,
Kenneth


Hi Kenneth,

in your example template "drawTable" , dis you actually mean to use

<xsl:for-each select="$nodes" >
<td>
<xsl:choose>
<xsl:when test="string-length($nodes) > 0">
<xsl:value-of select="$nodes"/>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$defaul tValue />
</xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>

so that the same "empty-string" test could be run for all empty nodes in
one for-each loop? I mean, assuming my original XML element structure,
would I then be able to use drawTable to replace all my empty elements
with this:

<xsl:call-template name="drawTable ">
<xsl:with-param name="nodes" select="record/*" />
<xsl:with-param name="defaultVa lue" select=" " />
</xsl:call-template>

so that record/elem1, record/elem2, record/elem3, record/elem4 would all
be search-replaced against the empty string?

Regards,
Jyrki
Jul 20 '05 #4

"Jyrki Keisala" <ji****@mail.su omi.net> wrote in message
news:Xn******** *************** *****@131.228.6 .99...

Hi Kenneth,

in your example template "drawTable" , dis you actually mean to use

<xsl:for-each select="$nodes" >
<td>
<xsl:choose>
<xsl:when test="string-length($nodes) > 0">
<xsl:value-of select="$nodes"/>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$defaul tValue />
</xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>

so that the same "empty-string" test could be run for all empty nodes in
one for-each loop? I mean, assuming my original XML element structure,
would I then be able to use drawTable to replace all my empty elements
with this:

<xsl:call-template name="drawTable ">
<xsl:with-param name="nodes" select="record/*" />
<xsl:with-param name="defaultVa lue" select=" " />
</xsl:call-template>

so that record/elem1, record/elem2, record/elem3, record/elem4 would all
be search-replaced against the empty string?

Hi,

Yes, that was the intent.

Regards,
Kenneth
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
62498
by: n_o_s_p_a__m | last post by:
My xml doc has many <title></title> and <title> in it, meaning the nodes have no content (although some do). How can I test for this? I tried title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) Any suggestions welcome.
3
1442
by: Jyrki Keisala | last post by:
I have an XML file which looks like this: <command name="Options" phrase="Options"> <key value="z" extended="NONE" qual="L-CTRL" pause="100" repeat="1" duration="200"/> </command> <command name="Command Room" phrase="Command Room"> <key extended="F2" pause="100" repeat="1" duration="200"/> </command>
10
23397
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of my depth with most of the posts, I will buy a good book and take some online tutorials) Anyway, I think I almost understand one of Mr Nielsen's posts on form validation. It all centers around whether I am interpreting an empty string correctly -...
23
4092
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
18
2476
by: Neal | last post by:
According to the specs (http://www.w3.org/TR/html401/struct/links.html#h-12.2), the <a> element requires an end tag. And so, when we use <A NAME="foo"> in HTML 2.0 to 4.01, it won't validate, it'll want to find the </A> tag. However, when I write a document containing, say, <a name="foo" /> it validates in XHTML 1.0. I'm obviously missing something here, as this confuses me somewhat. Does the magical / make the empty a element valid?...
2
26714
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as empty elements in the XML. How to do that ? I don't understand why there is no simple option to specify the output format, or did I miss something ? regards andreas
3
6357
by: Clark Spencer | last post by:
I have built a small integration app using VS .NET 2003 that extracts orderinformation from a 'webshop'. Extracting the orderinformation works fine. Appending the order elements in the XmlDocument was also done in a jiffy. The final step is to save the document to disk and then ship it to another system using ftp. The xml orderfile produced must fit a set specification of the recieving system. That specification states that empty elements...
7
34885
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>
3
2107
by: August Karlstrom | last post by:
Hi everyone, In XHTML, can any empty element <foo></foobe replaced by <foo />? Example: <table> <tr><td>1</td><td>2</td></tr> <tr><td /><td>4</td></tr> </table>
0
10146
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.