4

I have a form to fill out:

<form action="welcome.jsp"  method="post">
 <table>
  <tr><td>Email:</td><td><input type="text" name="email"></td></tr>
  <tr><td>Name:</td><td><input type="text" name="name"></td></tr>
  <tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr>
  <tr><td></td><td><input type="submit" value="Submit"></td></tr>
 </table>
</form>

However, how do I produce the same form using XSLT? This form resides in a index.jsp file and I have the xml in this file any mockup xml for now can be used, I am mostly confused in

<input ... > 

part.

thanks

Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47

2 Answers2

4

This XML input file:

<r>
  <email>bob@example.com</email>
  <name>Bob</name>
  <mobile>123-456-7890</mobile>
</r>

Fed to this XSLT transformation:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />
  <xsl:template match="/r">
    <xsl:variable name="email" select="email"/>
    <xsl:variable name="name" select="name"/>
    <xsl:variable name="mobile" select="mobile"/>
    <form action="welcome.jsp"  method="post">
      <table>
        <tr><td>Email:</td><td><input type="text" name="email" value="{$email}"></input></td></tr>
        <tr><td>Name:</td><td><input type="text" name="name" value="{$name}"/></td></tr>
        <tr><td>Mobile:</td><td><input type="text" name="mobile" value="{$mobile}"/></td></tr>
        <tr><td></td><td><input type="submit" value="Submit"/></td></tr>
      </table>
    </form>
  </xsl:template>
</xsl:stylesheet>

Yields this HTML of the completed form:

<form action="welcome.jsp" method="post">
   <table>
      <tr>
         <td>Email:</td>
         <td><input type="text" name="email" value="bob@example.com"></td>
      </tr>
      <tr>
         <td>Name:</td>
         <td><input type="text" name="name" value="Bob"></td>
      </tr>
      <tr>
         <td>Mobile:</td>
         <td><input type="text" name="mobile" value="123-456-7890"></td>
      </tr>
      <tr>
         <td></td>
         <td><input type="submit" value="Submit"></td>
      </tr>
   </table>
</form>

Which looks like this:

enter image description here

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

I was working with the data in the Oracle XE sample database. The XML for a single record is returned by DBMS_XMLGEN.GETXML as...

<ROWSET>
<ROW>
<CUSTOMER_ID>177</CUSTOMER_ID)
<NAME>United Continental Holdings</NAME>
<ADDRESS>2904 S Salina St, Syracuse, NY</ADDRESS>
<WEBSITE>http://www.unitedcontinentalholdings.com</WEBSITE>
<CREDIT_LIMIT>5000</CREDIT_LIMIT>
</ROW>
</ROWSET>

I wanted a generic style sheet which would convert any single record as above to a to a form with appropriate input types for each field. Here's the XSL...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous"></link>
</head>
<body>
    <form>
        <xsl:for-each select="ROWSET/ROW">
            <xsl:apply-templates/>
        </xsl:for-each>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</body>
</html>
</xsl:template>

<xsl:template match="*">
    <div class="row mb-3">

        <div class="col-sm-10">

            <label class="col-sm-2 form-label" for="name(.)"><xsl:value-of select="translate(name(.), '_', ' ')"/></label>

            <xsl:choose>

                <!-- numbers -->
                <xsl:when test="number(.)">
                    <input class="form-control" id="name(.)" name="name(.)" type="number" value="{.}"/>
                </xsl:when>

                <!-- dates -->
                <xsl:when test="translate(., '123456789', '000000000') = '0000-00-00'">
                    <input class="form-control" id="name(.)" name="name(.)" type="date" value="{.}"/>
                </xsl:when>

                <!-- email -->
                <xsl:when test="matches(upper-case($email),'^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$')">
                    <input class="form-control" id="name(.)" name="name(.)" type="email" value="{.}"/>
                </xsl:when>

                <!-- unless otherwise handled, it's type 'text' -->
                <xsl:otherwise>
                    <input class="form-control" id="name(.)" name="name(.)" type="text" value="{.}"/>
                </xsl:otherwise>

            </xsl:choose>

        </div>

    </div>
</xsl:template>

</xsl:stylesheet>

Luck.

Clarius
  • 1,183
  • 10
  • 10