创建xml属性值的下拉列表

时间:2013-04-01 05:23:26

标签: javascript html xml xslt xpath

这是我的xml文件: -

<child_2 entity_id="2" value="Root" parent_id="1">
    <child_4 entity_id="4" value="Activities" parent_id="2">
        <child_10066 entity_id="10066" value="Physical1" parent_id="4">
            <child_10067 entity_id="10067" value="Cricket" parent_id="10066">
                <child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
            </child_10067>
        </child_10066>
        <child_10069 entity_id="10069" value="Test2" parent_id="4"/>
        <child_10070 entity_id="10070" value="Test3" parent_id="4"/>
        <child_10071 entity_id="10071" value="Test4" parent_id="4"/>
        <child_10072 entity_id="10072" value="Test5" parent_id="4"/>
        <child_5 entity_id="5" value="Physical" parent_id="4"/>
    </child_4>
</child_2>

我想从child_4开始下拉列表,并且所有孩子都有这样的类型: -

<select>
    <option>Activities</option>
    <option>Physical1</option>
    <option>Cricket</option>
    <option>One DAy</option>
    <option>Test2</option>
    <option>Test3</option>
    <option>Test4</option>
    <option>Test5</option>
    <option>Physical</option>
</select><br>

这是我的xslt: -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:key name="kType" match="child_2" use="value"/>
  <xsl:template match="/">
  <select name="productGroup" id="productGroup">
      <xsl:apply-templates 
        select="child_2/child_4[generate-id() = 
                                 generate-id(key('kGroup', group)[1])]" />
    </select>
 </xsl:template>
 <xsl:template match="child_2">
 <option value="[@value]">
    <xsl:value-of select="@value"/>
</option>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

这是一个选项:

样式表

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

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

  <xsl:template match="child_4">
    <!--
    Retained @name and @id attributes because they were in your original XSLT code
    -->
    <select name="productGroup" id="productGroup">
      <!-- Apply the @value attribute on this element and all its descendants -->
      <xsl:apply-templates select="descendant-or-self::*/@value"/>
    </select>
  </xsl:template>

  <xsl:template match="@value">
    <option>
      <xsl:value-of select="."/>
    </option>
  </xsl:template>
</xsl:stylesheet>

输入

<child_2 entity_id="2" value="Root" parent_id="1">
  <child_4 entity_id="4" value="Activities" parent_id="2">
    <child_10066 entity_id="10066" value="Physical1" parent_id="4">
      <child_10067 entity_id="10067" value="Cricket" parent_id="10066">
        <child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
      </child_10067>
    </child_10066>
    <child_10069 entity_id="10069" value="Test2" parent_id="4"/>
    <child_10070 entity_id="10070" value="Test3" parent_id="4"/>
    <child_10071 entity_id="10071" value="Test4" parent_id="4"/>
    <child_10072 entity_id="10072" value="Test5" parent_id="4"/>
    <child_5 entity_id="5" value="Physical" parent_id="4"/>
  </child_4>
</child_2>

输出

<select name="productGroup" id="productGroup">
  <option>Activities</option>
  <option>Physical1</option>
  <option>Cricket</option>
  <option>One Day</option>
  <option>Test2</option>
  <option>Test3</option>
  <option>Test4</option>
  <option>Test5</option>
  <option>Physical</option>
</select>