p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   help on xslt transformation (http://p2p.wrox.com/showthread.php?t=66386)

li72 March 4th, 2008 09:57 AM

help on xslt transformation
 
Assuming we have an xml document like the one below.
<data>
   <node1 label="label1" type="integer" />
   <node2 label="label2" type="date" />
   <node3 label="label2" type="string" />
   </data>
 we need to transform this doc into XForms doc.
The model part looks like this
<model>
………………………………………… ………………….
………………………………………… ………………….
<xforms:bind nodeset="node1" required="true()" type="xs:integer"/>
<xforms:bind nodeset="node2" required="true()" type="xs:date"/>
<xforms:bind nodeset="node3" required="false()" type="xs:string"/>
</model>

for each node in xml document xslt processor should generates an XForms control element <xforms:input ……….> with link to the corresponding bind node. The label of each XForms control should take its value from the attribute [label].
The expected result it should like this

<p>
<xforms:input ref="node1">
<xforms:label> label1 </xforms:label>
</xforms:input> </p>
<p>
<xforms:input ref="node2">
<xforms:label> label2 </xforms:label>
</xforms:input> </p>
<p>
<xforms:input ref="node3">
<xforms:label> label3 </xforms:label>
</xforms:input> </p>

The question is how to write a template that generates this part of XForms dynamically?


Any help is much appreciated



Martin Honnen March 4th, 2008 10:12 AM

Your samples are not complete so the following stylesheet is not complete either but it shows you how to use modes to process nodes twice:
Code:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xforms="http://www.w3.org/2002/xforms">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="data">
    <html>
      <head>
        <title>Example</title>
        <xsl:apply-templates select="*" mode="binding"/>
      </head>
      <body>
        <xsl:apply-templates select="*" mode="controls"/>

      </body>
    </html>
  </xsl:template>

  <xsl:template match="data/*" mode="binding">
    <xforms:bind nodeset="{local-name()}" required="true()" type="xs:{@type}"/>
  </xsl:template>

  <xsl:template match="data/*" mode="controls">
    <p>
      <xforms:input ref="{local-name()}">
        <xforms:label><xsl:value-of select="@label"/></xforms:label>
      </xforms:input>
    </p>
  </xsl:template>

</xsl:stylesheet>


samjudson March 4th, 2008 10:14 AM

First, you seem to be posting multiple posts with almost identical subject lines - this is not good forum etiquette. Try to be a little bit more descriptive.

Secondly, its not clear from your post if this is one transformation (i.e. you want the input XML to be transformed twice in the same XSLT file) or if this is a two stage transformation.

It really sounds more like the former but from the previous posts you're written I'm suprised you can't work this out yourself.

Code:

<xsl:for-each select="/data/*">
<p>
<xforms:input ref="{local-name()}">
  <xforms:label><xsl:value-of select="@label"/></xforms:label>
</xforms:input>
</p>
</xsl:for-each>

If that isn't what you want then try showing us what you've tried so far and why it doesn't work.

/- Sam Judson : Wrox Technical Editor -/


All times are GMT -4. The time now is 09:10 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.