本文介绍了使用xml和可重用的xslt动态生成HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的xml文件:

第一:

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

第二:

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

等等。

不会有更多嵌套标签。
但是对于每个xmldata文件,property标签的名称是不同的。

所以我想动态生成一个 HTML 使用 XSLT 的表单用于读取每个 xml 的数据。如果应该使用简单的文本框来读取每个属性。我们可以把第一条记录作为参考数字和名称的属性。



所需输出

 < form name =xmldata1> 
< table>
< tr>
< td> property11:< / td>
< td>< input type =textname =property11>< / td>
< / tr>
.......
等等
< / table>
< / form>

我该如何实现这一点。我在哪里可以找到这样的示例示例。

解决方案
 <?xml version = 1.0\" >?; 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =htmlindent =yes/>

< xsl:template match =/ *>
< form name ={local-name()}>
< table>
< xsl:apply-templates select =* / */>
< / table>
< / form>
< / xsl:template>

< xsl:template match =/ * / * / *>
< tr>
< td>< xsl:value-of select =local-name()/> :其中; / TD>
< td>< input type =textname ={local-name()}/>< / td>
< / tr>
< / xsl:template>

< / xsl:stylesheet>


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.

解决方案
<?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>

这篇关于使用xml和可重用的xslt动态生成HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:57