tags:

views:

86

answers:

2

I have 20 forms which I am printing using XML and XSLT. Now I need a functionality where I can print these forms as blank. For example I have a "Name" field in my XML and a corresponding

<span>Name:</span>
<strong class="data">
    <xsl:value-of disable-output-escaping="yes" select="Name" />
</strong>

I need 2 functionalities:

  1. I want to print the Name e.g.: Name: John Doe
  2. I don't want to print the Name and let the user fill in e.g.: Name:________

Depending on whether its a text box, text area, or check box, I want to add relative spaces. So, if it's a text area field I want to add a space equal to the field size so that the user can add information.

Does anybody have any idea how this can be implemented?

Thanks

A: 

You could use the string-length function to determine the length of your name string, and if it is blank, then you print your underline string. here's a sample:

<span>Name:</span>
<strong class="data">
    <xsl:choose>
        <xsl:when test="string-length(Name) > 0">
            <xsl:value-of disable-output-escaping="yes" select="Name" />
        </xsl:when>
        <xsl:otherwise>
            _____________________________
        </xsl:otherwise>
    </xsl:choose>
</strong>

This way, you just pass in a blank XML document, and the stylesheet will print it as such.

Steve
Thanks for the reply, But I don't want to go through all my xslts(there are some 80 odd) and make this change. Plus if there is a text area then I need a couple of line spaces.
Mithil Deshmukh
+1  A: 

To create blanks of the appropriate length, you can either:

<!-- make sure you put in more blanks than you'll need -->
<xsl:variable name="blanks" select="'___[...100 blanks...]___'" />

<!-- and later -->

<xsl:value-of select="substring($blanks, 1, @fieldlength)" />

Or

<xsl:template name="create-blanks">
  <xsl:param name="todo" select="0" />

  <xsl:if test="$todo &gt; 0">
    <xsl:text>_</xsl:text>
    <xsl:call-template name="create-blanks">
      <xsl:with-param name="todo" select="$todo - 1" />
    </xsl:call-template>
  </xsl:choose>
</xsl:template>

<!-- and later -->

<xsl:variable name="blanks">
  <xsl:call-template name="create-blanks">
    <xsl:with-param name="todo" select="@fieldlength" />
  </xsl:call-template>
</xsl:variable>

<xsl:value-of select="$blanks" />

Performance-wise, the first approach is clearly to be favored. The latter can be somewhat improved by creating more than one blank (e.g. 10) each time and calling it not @fieldlength, but ceiling(@fieldlength div 10) times, using substring() on the result. This calculation can of course happen in the template itself.

If no sensible maximum of blanks can be pre-determined (somewhat improbable for your case, I guess), the latter method ensures you can never "run out".

Tomalak
Thanks for the reply, But I don't want to go through all my xslts(there are some 80 odd) and make this change. Plus if there is a text area then I need a couple of line spaces.
Mithil Deshmukh
So what exactly *do* you want? How do you want to change the output if you are not going to change the XSLTs?
Tomalak
basically what I was thinking is i create a blank xml for all my forms and then depending on the fieldsize of each field I add corresponding number of blank spaces. Does that make any sense?
Mithil Deshmukh
Not yet. I see the dots, but I can't connect them. What you you have (sample code?) and, going from there, what do you need?
Tomalak