4

I have large number of xml files :

First:

<xmldata1>
    <record>
        <property11>abc</property11>
        <property12>def</property12>
        <property13>xyz</property13>
        ............
    </record>
    ........
</xmldata1>

Second:

<xmldata2>
    <record>
        <property21>abc</property21>
        <property22>def</property22>
        <property23>xyz</property23>
        ............
    </record>
    ........
</xmldata2>

and so on.

There won't be any more nested tags . But the name of property tag will be different for each xmldata file.

So i want to dynamically generate a HTML Form using XSLT to be used to read data for each xml . Where a simple text box should be used to read each property. and we can take the first record as reference for number and name of properties.

The desired output

<form name ="xmldata1">
    <table>
        <tr>
            <td>property11 :</td>
            <td><input type="text" name="property11"></td>
        </tr>
        .......
        and so on
    </table>
</form>

How can i achieve this. Where can i find sample example for this.

1
  • Would be good to provide an example of what you've attempted and not expecting the community to write this for you, that is not the purpose of Stack Overflow. Commented Sep 20, 2011 at 19:25

2 Answers 2

5
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />

    <!--Template match for the document element, no matter what the name -->
    <xsl:template match="/*">
      <form name="{local-name()}">
        <table>
           <!--Apply-templates for all of the record/property elements -->
           <xsl:apply-templates select="*/*"/>
         </table>
      </form>
    </xsl:template>

    <!--Match on all of the property elements and create a new row for each -->
    <xsl:template match="/*/*/*">
      <tr>
        <td><xsl:value-of select="local-name()"/> :</td>
        <td><input type="text" name="{local-name()}"/></td>
      </tr>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

3

Do you mean sth like that?

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" />

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

<xsl:template match="*[starts-with(name(), 'xmldata')]">
    <xsl:element name="form">
        <xsl:attribute name="name"><xsl:value-of select="name(.)" /></xsl:attribute>
        <table>
            <tr>
                <xsl:apply-templates />
            </tr>
        </table>
    </xsl:element>
</xsl:template>

<xsl:template match="record">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="*[starts-with(name(), 'property')]">
    <td>
        <xsl:value-of select="name(.)" />
    </td>
    <td>
        <xsl:element name="input">
            <xsl:attribute name="type">text</xsl:attribute>
            <xsl:attribute name="name"><xsl:value-of select="name(.)" /></xsl:attribute>
        </xsl:element>
    </td>
</xsl:template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.