通过属性显示特定标签中的项目

时间:2017-03-14 02:01:22

标签: asp.net xml xslt

基本上我现在想做的是显示特定标签中的项目。我打算用唯一属性区分标签。我现在面临的问题是我似乎没有工作,因为它仍然显示来自另一个标签的项目。请帮忙。

这是XML内容

<group name="Personal Particulars">
  <field is_admin_field="N" required="Y">
    <question_title>Name</question_title>
    <type>TextField</type>
    <db_field_name>name</db_field_name>
    <db_field_length>250</db_field_length>
    <db_field_type>string</db_field_type>
    <additional_comment/>
  </field>
  <field is_admin_field="N" required="Y">
    <question_title>NRIC/FIN</question_title>
    <type>TextField</type>
    <db_field_name>nricfin</db_field_name>
    <db_field_length>20</db_field_length>
    <db_field_type>string</db_field_type>
    <additional_comment/>
  </field>
  <field is_admin_field="N" required="Y">
    <question_title>Contact No.</question_title>
    <type>TextField</type>
    <db_field_name>contact_no</db_field_name>
    <db_field_length>20</db_field_length>
    <db_field_type>string</db_field_type>
    <additional_comment/>
  </field>
</group>

<group name="Housing Type">
  <field is_admin_field="N" required="Y">
    <question_title>Which of the housing type best describes your residential?</question_title>
    <type>List</type>
    <db_field_name>which_of_the_housing_type_best_describes_your_residential</db_field_name>
    <options>
      <item score="0">3 - 5 room HDB</item>
      <item score="0">Executive Condominium </item>
      <item score="0">Landed 1 Floor</item>
      <item score="0">Landed 2 Floor</item>
      <item score="0">Landed 3 Floor</item>
      <item score="0">Landed 4 Floor</item>
      <item score="0">Landed 5 Floor</item>
    </options>
    <db_field_length>22</db_field_length>
    <additional_comment/>
  </field>
</group>

这是XSLT

<div style="border:1px solid black">
    <div style="border:1px solid black">
      <xsl:variable name="name" select="/form/fieldset/group/@name"/>
      <xsl:if test="$name='Personal Particulars'">
        <xsl:text>Personal Particulars</xsl:text>
        <xsl:for-each select="form/fieldset/group/field">
    <div>
      <xsl:value-of select="question_title"/>
    </div>
    <xsl:variable name="type" select="type"/>
    <xsl:if test="$type='TextField'">
      <xsl:element name="input">
        <xsl:attribute name="type">textbox</xsl:attribute>
        <xsl:attribute name="maxlength">5</xsl:attribute>
      </xsl:element>
    </xsl:if>
    </xsl:for-each>
      </xsl:if>
  </div>
    <div style="border:1px solid black">
      <xsl:variable name="name" select="/form/fieldset/group/@name"/>
      <xsl:if test="$name='Housing Type'">
        <xsl:text>Housing Type</xsl:text>
        <xsl:for-each select="form/fieldset/group/field">
          <div>
            <xsl:value-of select="form/fieldset/group/question_title"/>
          </div>
          <xsl:variable name="type" select="type"/>
          <xsl:if test="$type='TextField'">
            <xsl:element name="input">
              <xsl:attribute name="type">textbox</xsl:attribute>
              <xsl:attribute name="maxlength">5</xsl:attribute>
            </xsl:element>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
    </div>
  </div>

1 个答案:

答案 0 :(得分:1)

试试这个。您必须在for-each循环中调整XPath以匹配实际的XML结构 -

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="html" encoding="utf-8" indent="yes" />
   <xsl:template match="/">
      <xsl:for-each select="item">
         <div style="font-size:150%;margin-top:5%;">
            <xsl:value-of select="title"/>
         </div>
         <div style="font-size:150%;">
            <xsl:value-of select="description"/>
         </div>
         <xsl:variable name="category" select="category"/>
         <xsl:if test= "$category = 'Text' " >
            <xsl:for-each select="./options/need">
               <div>
                  <xsl:element name="input">
                     <xsl:attribute  name="type">checkbox</xsl:attribute>
                     <xsl:attribute name="value">
                        <xsl:value-of select="need"/>
                     </xsl:attribute>
                  </xsl:element>
                  <label>
                     <xsl:value-of select="."/>
                  </label>
               </div>
            </xsl:for-each>
         </xsl:if>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

OUTPUT -

enter image description here

拥有像<xsl:variable name="category" select="category"/>之类的变量可以让你在嵌套的for-each循环中将条件<xsl:if test= "$category = 'Text' " >放在任何你想要的地方

根据后续评论,添加如何显示单选按钮 -

<xsl:element name="input">
   <xsl:attribute  name="type">radio</xsl:attribute>
   <xsl:attribute  name="name">something</xsl:attribute>
   <xsl:attribute name="value">
      <xsl:value-of select="need"/>
   </xsl:attribute>
   <xsl:value-of select="."/>
</xsl:element>
<label>
   <xsl:value-of select="."/>
</label>