Solved

I need a generic XSLT stylesheet to transform to XForms document

Posted on 2007-11-16
11
933 Views
Last Modified: 2013-11-18
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.
0
Comment
Question by:askanyquestions123
  • 7
  • 3
11 Comments
 

Author Comment

by:askanyquestions123
ID: 20303511
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

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20303626
what is the point asking this question again if you don't answer the questions asked in your previous question?
0
 

Author Comment

by:askanyquestions123
ID: 20303637
Well you can always check out the examples. You can alos try getting more from;
http://www.xsmiles.org/demo/demos.xml
0
 

Author Comment

by:askanyquestions123
ID: 20303646
<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>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20303897
my last post in
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XSLT/Q_22955197.html
explains why this is not enough

cheers

Geert
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:askanyquestions123
ID: 20314208
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.
0
 

Author Comment

by:askanyquestions123
ID: 20321614
Hi there,

I am waiting for your reply.
Thank you.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20356406
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
0
 

Author Comment

by:askanyquestions123
ID: 20367416
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

0
 

Author Closing Comment

by:askanyquestions123
ID: 31409712
Closing this questions, as I have another open question, for which I am awaiting reply.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now