[xsl] Grouping into a table (for vertical alignment)

Subject: [xsl] Grouping into a table (for vertical alignment)
From: "Daniel Joshua" <daniel.joshua@xxxxxxxxxxxx>
Date: Mon, 24 May 2004 21:11:39 +0800
Hi,

I am trying to make a generic XSLT for converting XML into XHTML.

I want to put my output into a <table> for vertically aligning the 'label'
and 'input' fields.

Here's a simplified example...

XML (Input):

.
.
.
  <form>
    <name>form</name>
    <action>submit.do</action>
    <method>post</method>
    <content>

      <text>
        <value>Please enter your Name and Password.</value>
        <class>instruction</class>
      </text>

      <input>
        <name>username</name>
        <label>Name: </label>
        <value></value>
        <class>mandatory</class>
      </input>

      <password>
        <name>password</name>
        <label>Password :</label>
        <value></value>
        <class>mandatory</class>
      </password>

      <text>
        <value>All attempts are logged.</value>
        <class>warning</class>
      </text>
    </content>
  </form>
.
.
.

XSLT:

.
.
.
  <xsl:template match="form">
    <form>
      <xsl:for-each select="name | action | method">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>

      <xsl:apply-templates"/>
    </form>
  </xsl:template>



  <xsl:template match="text">
    <div>
      <xsl:for-each select="class">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>

      <xsl:value-of select="value"/>
    </div>
  </xsl:template>



  <xsl:template match="label">
    <xsl:value-of select="."/>
    <xsl:text>: </xsl:text>
  </xsl:template>



  <xsl:template match="input">
    <xsl:apply-templates select="label"/>

    <input>
      <xsl:attribute name="type">
        <xsl:text>text</xsl:text>
      </xsl:attribute>

      <xsl:for-each select="name | value | class">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
    </input>
  </xsl:template>



  <xsl:template match="password">
    <xsl:apply-templates select="label"/>

    <input>
      <xsl:attribute name="type">
        <xsl:text>password</xsl:text>
      </xsl:attribute>

      <xsl:for-each select="name | value | class">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
    </input>
  </xsl:template>
.
.
.

XHTML (Output):

.
.
.
<form name="form" action="submit.do" method="post">
<div class="instruction">Please enter your Name and Password.</div>
Name: <input type="text" name="username" value=""
class="mandatory">Password: <input type="password" name="password" value=""
class="mandatory">
<div class="warning">All attempts are logged..</div>
.
.
.


Regards,
Daniel

Current Thread