473,395 Members | 1,516 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,395 software developers and data experts.

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:otherwise>
 
</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 4019
On Thu, 1 Jan 2004 10:24:58 +0000 (UTC), Jyrki Keisala
<wh*****@wherever.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*****@wherever.com> wrote in message
news:Xn********************************@212.50.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:otherwise>
 
</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="defaultValue" />
<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="$defaultValue"
/></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="defaultValue" 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="defaultValue" />
<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="$defaultValue"
/></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="defaultValue" 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:otherwise>
<xsl:value-of select="$defaultValue />
</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="defaultValue" 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.suomi.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:otherwise>
<xsl:value-of select="$defaultValue />
</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="defaultValue" 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
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)...
3
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...
10
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...
23
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...
18
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,...
2
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...
3
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...
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...
3
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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.