Defaulting empty XML elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jyrki Keisala

    #1

    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>&#160;</td>
    <td>&#160;</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>
    &#160;
    </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
  • Andy Dingley

    #2
    Re: Defaulting empty XML elements

    On Thu, 1 Jan 2004 10:24:58 +0000 (UTC), Jyrki Keisala
    <whoever@wherev er.com> wrote:
    [color=blue]
    >It seems a bit tedious to use the structure[/color]

    XSLT _is_ tedious. Get used to it, or look at ways of automatically
    creating it.
    --
    Klein bottle for rent. Apply within.

    Comment

    • Kenneth Stephen

      #3
      Re: Defaulting empty XML elements


      "Jyrki Keisala" <whoever@wherev er.com> wrote in message
      news:Xns94637E3 E127FEjiikoomai lsuominet@212.5 0.131.130...
      [color=blue]
      > 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>&#160;</td>
      > <td>&#160;</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>
      > &#160;
      > </xsl:otherwise>
      > </xsl:choose>
      > </td>
      >[/color]

      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="&#160;" />
      </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


      Comment

      • Jyrki Keisala

        #4
        Re: Defaulting empty XML elements

        > Hi,[color=blue]
        >
        > 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="&#160;" />
        > </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
        >
        >[/color]

        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="&#160;" />
        </xsl:call-template>

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

        Regards,
        Jyrki

        Comment

        • Kenneth Stephen

          #5
          Re: Defaulting empty XML elements


          "Jyrki Keisala" <jiikoo@mail.su omi.net> wrote in message
          news:Xns9464620 3DE271zoltanjip piifi@131.228.6 .99...[color=blue]
          >
          > 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="&#160;" />
          > </xsl:call-template>
          >
          > so that record/elem1, record/elem2, record/elem3, record/elem4 would all
          > be search-replaced against the empty string?
          >[/color]
          Hi,

          Yes, that was the intent.

          Regards,
          Kenneth


          Comment

          Working...