Defaulting empty XML elements

Discussion in 'XML' started by Jyrki Keisala, Jan 1, 2004.

  1. 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:eek:therwise>
     
    </xsl:eek:therwise>
    </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
    Jyrki Keisala, Jan 1, 2004
    #1
    1. Advertising

  2. Jyrki Keisala

    Andy Dingley Guest

    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.
    Andy Dingley, Jan 1, 2004
    #2
    1. Advertising

  3. "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>
    > <xsl:eek:therwise>
    >  
    > </xsl:eek:therwise>
    > </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:eek:therwise><xsl:value-of select="$defaultValue"
    /></xsl:eek:therwise>
    </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
    Kenneth Stephen, Jan 1, 2004
    #3
  4. > 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:eek:therwise><xsl:value-of select="$defaultValue"
    > /></xsl:eek:therwise>
    > </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:eek:therwise>
    <xsl:value-of select="$defaultValue />
    </xsl:eek:therwise>
    </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
    Jyrki Keisala, Jan 2, 2004
    #4
  5. "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>
    > <xsl:eek:therwise>
    > <xsl:value-of select="$defaultValue />
    > </xsl:eek:therwise>
    > </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
    Kenneth Stephen, Jan 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    536
  2. Jeremy Chapman

    Custom control, defaulting style properties

    Jeremy Chapman, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    Jeremy Chapman
    Mar 7, 2006
  3. Jyrki Keisala

    Defaulting empty attributes to nbsp

    Jyrki Keisala, Jun 14, 2005, in forum: XML
    Replies:
    3
    Views:
    474
    Jyrki Keisala
    Jun 20, 2005
  4. Lukas
    Replies:
    3
    Views:
    783
    spiff
    Nov 10, 2005
  5. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    400
Loading...

Share This Page