Link to home
Start Free TrialLog in
Avatar of askanyquestions123
askanyquestions123

asked on

I need a generic XSLT stylesheet to transform to XForms document

Consider an XML document like the one below. How can we write a generic XSLT stylesheet to transform this document to an XForms document? Transformation is based on the value of <type> and <nullable> elements. i.e all information we have is the data type of element and whether it accepts null values or not. In addition, the label of Xforms element form takes its value from <label> node.
Please bear in mind that the XSLT stylesheet must bee generic. i.e Reusable for any XML document with any number of <element> nodes.

<login>
   <element>
           <label> User ID </label>
          <type> integer </type>
           <nullable> false </nullable>
   </element>
   <element > 
             <label> Password </label>
            <type> string </type>
            <nullable> false </nullable>
   </element>
   <element > 
             <label> D.o.Birth </label>
            <type> date </type>
            <nullable> true </nullable>
   </element>
</login>

Each XML node must be mapped to a specific XForms UI element. Mapping should be based on the <type> node. i.e if <type> is string or integer it should be mapped to <input ref=”string”…>. If type is <boolean> then mapped it to <select1>. If it is date then it should be mapped to <input ref=”date”…>. Also, node <nullable> … </nullable> must be used to validate the entry date null or not

I tried writing this and I have the following;

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="element[normalize-space(type) = 'string']">
<input ref="string">
<label><xsl:value-of select="normalize-space(label)"/></label>
<bind nodeset="/login/element">
<xsl:attribute name="required">
<xsl:apply-templates select="nullable"/>
</xsl:attribute>
</bind>
</input>
</xsl:template>

<xsl:template match="element[normalize-space(type) = 'integer']">
<input ref="string">
<label><xsl:value-of select="normalize-space(label)"/></label>
<bind nodeset="/login/element">
<xsl:attribute name="required">
<xsl:apply-templates select="nullable"/>
</xsl:attribute>
</bind>
</input>
</xsl:template>

<xsl:template match="element[normalize-space(type) = 'boolean']">
<select1 ref="boolean">
<bind nodeset="/login/element">
<xsl:attribute name="required">
<xsl:apply-templates select="nullable"/>
</xsl:attribute>
</bind>
</select1>
</xsl:template>

<xsl:template match="element[normalize-space(type) = 'date']">
<input ref="date">
<label><xsl:value-of select="normalize-space(label)"/></label>
<bind nodeset="/login/element">
<xsl:attribute name="required">
<xsl:apply-templates select="nullable"/>
</xsl:attribute>
</bind>
</input>
</xsl:template>

<xsl:template match="null">
<xsl:choose>
<xsl:when test="normalize-space(.) = 'false'">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>false</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


However, when I transfer the xml doc using the above xslt stylesheet, I get an xml doc and not XForms doc. (you can do a View Source to see this)
So, I think the above xslt stylesheet needs some extra work to consider the following
a) we need to build up the <xforms:model> which contains the <xforms:instance> and binding each instance elemet to a specific xforms control element.
b) The xforms control elements must be placed between <body> and </body> tags.

Thus, basicall I require a complete xslt document (based on the above doc), which includes
<xsl:output method =”xhtml” /> and placing each part of it between the right tags (<html> <head> <body>).

The Platform is Windows XP and the browser is X-smiles.

Thanks in advance for all your help.
Avatar of askanyquestions123
askanyquestions123

ASKER

I am readding the code related information, which is not clearly visible above;

each XML node must be mapped to a specific XForms UI element. Mapping should be based on the <type> node. i.e if <type> is string or integer it should be mapped to <input ref=”string”…>. If type is <boolean> then mapped it to <select1>. If it is date then it should be mapped to <input ref=”date”…>. Also, node <nullable> … </nullable> must be used to validate the entry date null 
-------------------------------------------
 
Thus, basically I require a complete xslt document (based on the above doc), which includes
<xsl:output method =”xhtml” /> and placing each part of it between the right tags (<html> <head> <body>).

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well you can always check out the examples. You can alos try getting more from;
http://www.xsmiles.org/demo/demos.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:html="http://www.w3.org/1999/xhtml"
 xmlns:xfm="http://www.w3.org/2000/12/xforms" version="1.0">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/*">
      <html:html>
            <html:head>
                  <html:link href="controls.css" rel="stylesheet" type="text/css"/>
                  <html:style type="text/css">              
                        .section {background-color: #cccccc; border-width:2px; margin-bottom: 10px; padding-bottom: 5px;margin-left:30px;border-style:solid;}
                        h1 {font-size:18px; color:blue; margin-bottom: 8px;}
                        h2 {align: center;background-color:blue; color:white; margin-bottom: 3px; padding-left: 5px;margin-top:0px;}
                        .bookmark {padding-left: 10px; font-weight: bold; margin-bottom: 1px; margin-top: 1px;}
                  </html:style>
            </html:head>
            <html:body>
                  <html:h1>My Bookmarks</html:h1>
                  <xsl:apply-templates select="document('bookmarks2.xml')/bookmarks/section"/>
            </html:body>
      </html:html>
      </xsl:template>
      <!-- *************** section *******************-->
      <xsl:template match="section">
      <html:div class="section">
            <html:h2><xsl:value-of select="@name"/></html:h2>
            <xsl:apply-templates select="bookmark|section"/>
      </html:div>
      </xsl:template>
      <!-- *************** bookmark *******************-->
      <xsl:template match="bookmark">
            <html:p class="bookmark"><a href="{@href}" target="documentwindow"><xsl:value-of select="@name"/></a>
            </html:p>
      </xsl:template>
</xsl:stylesheet>



************

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/*">
    <html>
      <head>
      <link rel="stylesheet" type="text/css" href="player.css" />
      </head>
      <body>
      <p>
        <xsl:apply-templates select="img|name|nationality|weight|height|goals|birthdate|caps|club"/>
      </p>
      <p>
        <xsl:apply-templates select="bio"/>
      </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="name">
     <b><xsl:value-of select="."/></b><br/>
  </xsl:template>

  <xsl:template match="img">
    <img src="{@src}" alt=""/><br/>
  </xsl:template>

  <xsl:template match="nationality"><b>Nationality:</b> <xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="position"><b>Position: </b><xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="height"><b>Height: </b><xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="weight">
      <b>Weight: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="goals">
      <b>Goals: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="caps">
      <b>Caps: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="club">
      <b>Club: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="birthdate">
    <b>Birthdate: </b><xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="bio">
      <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
******************************************

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/*">
    <html>
      <head>
      <link rel="stylesheet" type="text/css" href="player.css" />
      </head>
      <body>
      <p>
        <xsl:apply-templates select="img|name|nationality|weight|height|goals|birthdate|caps|club"/>
      </p>
      <p>
        <xsl:apply-templates select="bio"/>
      </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="name">
     <b><xsl:value-of select="."/></b><br/>
  </xsl:template>

  <xsl:template match="img">
    <img src="{@src}" alt=""/><br/>
  </xsl:template>

  <xsl:template match="nationality"><b>Nationality:</b> <xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="position"><b>Position: </b><xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="height"><b>Height: </b><xsl:value-of select="."/><br/></xsl:template>

  <xsl:template match="weight">
      <b>Weight: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="goals">
      <b>Goals: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="caps">
      <b>Caps: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="club">
      <b>Club: </b><xsl:value-of select="."/><br/>
  </xsl:template>

  <xsl:template match="birthdate">
    <b>Birthdate: </b><xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="bio">
      <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
my last post in
https://www.experts-exchange.com/questions/22955197/XForms.html
explains why this is not enough

cheers

Geert
Hello,

I have 2 examples. The first is very simple. But, the most important is the second one. However, the only thing to keep inmind is that the second example uses a static XSLT stylesheet whereas I am looking for a generic one. In addition, the structure of XML doc is slightly different.

http://xforms-examples.googlecode.com/svn/trunk/01-intro/01.hello-world/01-hello-world.xhtml

http://www.stylusstudio.com/xsllist/200304/post60900.html#

Thank you.
Hi there,

I am waiting for your reply.
Thank you.
well, I have been away for some time,
you can pose this question another ten times,
it will only help if you give the right information

The examples you post are not really helpfull
All I am asking is an XHTML document that works for you in XSmiles
not an XSLT, not something else

What I expect you to do is a little effort,
give me an example of an XForms that works in XSmiles,
one that you construct manually
If you do that, I can see the namespaces and give you the XSLT

There are some flavours of XForms and bindings and there are specific styles per framework
I am not going to figure out what will work for you in XSmiles,
you should do that yourself.
If you can't do that, I am afraid I can't help you

You have about a dozen open questions that are more or less the same,
but you fail to answer simple questions in return
Please understand that the ball is in your camp and that opening another dozen questions will not help you further in getting an answer

cheers

Geert
Here is a very simple XForms doc which is constructed manually.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">-->

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:my="test" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<head>

<title>Hello XForms!</title>

<xforms:model id="form1">

<xforms:instance xmlns="">

<Name xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:my="test" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

</xforms:instance>

</xforms:model>

</head>

<body>

<xforms:input ref="/Name">

<xforms:label>Type your name in the box and press tab: </xforms:label>

</xforms:input>

<p>

<xforms:output value="concat('Hello ', /Name)">

<xforms:label>Output: </xforms:label>

</xforms:output>

</p>

</body>

</html>

However, in our case:
1- Instance data must be constructed and placed in <xforms:model> part.
2- Each XForms control must be binding to instance data node.
3- <xsl:for-each> loop is needed to loop through the XML doc in order to construct the instance data and XForms controls.
4- Mapping each XML node depends on <type> XXXX </type> node. Let us assume the rules for demonstration purpose.
· If <type> date </type> map it to <xforms:input ref="date"> <xforms:label> columnName </xforms:label> ...
</xforms:input>

· If <type> boolean </type> map it to <xforms:select1 ref="XXX"> <xforms:label> columnName </xforms:label> ... </xforms:select1>

If we success to generate the required doc. Countless rules can be added with little effort.
I hope that these hints are useful.


Another example: http://www.xsmiles.org/demo/testsuite/xforms/controls.xhtml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">-->
 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:my="test" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
<head>
 
<title>Hello XForms!</title>
 
<xforms:model id="form1">
 
<xforms:instance xmlns="">
 
<Name xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:my="test" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
 
</xforms:instance>
 
</xforms:model>
 
</head>
 
<body>
 
<xforms:input ref="/Name">
 
<xforms:label>Type your name in the box and press tab: </xforms:label>
 
</xforms:input>
 
<p>
 
<xforms:output value="concat('Hello ', /Name)">
 
<xforms:label>Output: </xforms:label>
 
</xforms:output>
 
</p>
 
</body>
 
</html>

Open in new window

Closing this questions, as I have another open question, for which I am awaiting reply.