Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Defaulting empty XML elements

Reply
Thread Tools

Defaulting empty XML elements

 
 
Jyrki Keisala
Guest
Posts: n/a
 
      01-01-2004
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>
<xsltherwise>
 
</xsltherwise>
</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
 
Reply With Quote
 
 
 
 
Andy Dingley
Guest
Posts: n/a
 
      01-01-2004
On Thu, 1 Jan 2004 10:24:58 +0000 (UTC), Jyrki Keisala
<> 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.
 
Reply With Quote
 
 
 
 
Kenneth Stephen
Guest
Posts: n/a
 
      01-01-2004

"Jyrki Keisala" <> wrote in message
news:Xns94637E3E127FEjiikoomailsuominet@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>
> <xsltherwise>
>  
> </xsltherwise>
> </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">
<xslaram name="nodes" />
<xslaram name="defaultValue" />
<xsl:for-each select="$nodes">
<td>
<xsl:choose>
<xsl:when test="string-length(elem1) > 0">
<xsl:value-of select="elem1"/>
</xsl:when>
<xsltherwise><xsl:value-of select="$defaultValue"
/></xsltherwise>
</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


 
Reply With Quote
 
Jyrki Keisala
Guest
Posts: n/a
 
      01-02-2004
> 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">
> <xslaram name="nodes" />
> <xslaram name="defaultValue" />
> <xsl:for-each select="$nodes">
> <td>
> <xsl:choose>
> <xsl:when test="string-length(elem1) > 0">
> <xsl:value-of select="elem1"/>
> </xsl:when>
> <xsltherwise><xsl:value-of select="$defaultValue"
> /></xsltherwise>
> </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>
<xsltherwise>
<xsl:value-of select="$defaultValue />
</xsltherwise>
</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
 
Reply With Quote
 
Kenneth Stephen
Guest
Posts: n/a
 
      01-02-2004

"Jyrki Keisala" <> wrote in message
news:Xns94646203DE271zoltanjippiifi@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>
> <xsltherwise>
> <xsl:value-of select="$defaultValue />
> </xsltherwise>
> </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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom control, defaulting style properties Jeremy Chapman ASP .Net 0 03-07-2006 07:35 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Defaulting empty attributes to nbsp Jyrki Keisala XML 3 06-20-2005 07:22 AM
?? dropdownlist in datagrid, not defaulting to current value ??? THANK YOU jason@cyberpine.com ASP .Net 2 02-16-2005 03:10 PM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57