【问题标题】:Generate HTML form dynamically using xml and reusable xslt使用 xml 和可重用的 xslt 动态生成 HTML 表单
【发布时间】:2011-09-20 19:22:46
【问题描述】:

我有大量的 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 文件的属性标签名称会有所不同。

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

想要的输出

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

我怎样才能做到这一点。我在哪里可以找到这方面的示例。

【问题讨论】:

  • 最好提供一个您尝试过的示例并且不期望社区为您编写此内容,这不是 Stack Overflow 的目的。

标签: html xml xslt


【解决方案1】:
<?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>

【讨论】:

    【解决方案2】:

    你是那个意思吗?

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      相关资源
      最近更新 更多