XML,XSLT and ASP.net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: XML,XSLT and ASP.net

  1. #1
    Join Date
    Jun 2006
    Posts
    7

    XML,XSLT and ASP.net

    Hi,

    I need to render the XML document in a ASPX page, using XSLT. I am not able to handle multiple if conditions in the xslt file.
    For example:

    <choice value="Yes" name="yes" variable="choice">
    <question type="lbl" value="Please select one or more sub-domains"></question>
    <question type="checkbox" name="Perform Research and Design" ></question>
    <question type="checkbox" name="Create and Manage Product Data" ></question>
    <question type="checkbox" name="Manage Product Lifecycle"></question>
    </choice>
    <choice value="No"></choice>

    I have rendered the choice as a dropdown in the XSLT. But I should place a condition, on the value 'Yes' in the dropdown, the checkbox should appear..

    This is my XSLT file.

    <xsl:choose>
    <xsl:when test="$choice='Yes'">
    <xsl:for-each select="question/choice/question">
    <xsl:if test="@type='lbl'">
    <asp:Label Cssclass="styletextsublabel1" runat="server"><xsl:value-of select="@value"></xsl:value-of></asp:Label>
    </xsl:if>
    </xsl:for-each>

    <xsl:if test="@type='checkbox'">
    <asp:Checkbox runat="server" Cssclass="stylepadding">
    <xsl:for-each select="question">
    <xsl:value-of select="@value" />
    </xsl:for-each>
    </asp:Checkbox>
    </xsl:if>
    </xsl:when>
    </xsl:choose>

    But it doesn't work..Can anybody help me out in resolving this..

  2. #2
    Join Date
    Jun 2006
    Posts
    9
    What does "it doesn't work" mean? What are you expectingt he XSLT to generate and what is it actually generating? While I know little of Asp.Net I am comfortable with XSLT. If you give me some hints at what the generated output looks like vs. what it should look like I can help.

  3. #3
    Join Date
    Jun 2006
    Posts
    7
    Hi,

    Thanks for the response. My Requirement is ,

    I have an XML file which is like a survey. (ie) Question and Answers. Certain questions will appear based on the conditions.

    <survey name="Scope">
    <question type="label" name="Question1?" id="lblSQ1"></question>
    <question type="dropdown" id="ddlSQ2">
    <choice value="Yes"></choice>
    <choice value="No"></choice>
    </question>

    <question type="label" name="Question2" id="lblSQ2">
    </question>
    <question type="label" name="Please select all that apply."></question>
    <question type="label" name="Question 2A?" id="lblSQ2a">
    </question>

    <question type="dropdown" id="ddlSQ2a">
    <choice value=""></choice>
    <choice value="Yes" name="Yes" id="Yes">
    <question type="lbl" name="Please select one or more sub-domains"></question>
    <question type="checkbox" name="Perform Research and Design" ></question>
    <question type="checkbox" name="Create and Manage Product Data" ></question>
    <question type="checkbox" name="Manage Product Lifecycle"></question>
    </choice>
    <choice value="No"></choice>
    </question>


    Here it is.. I need to render the Question and the corresponding controls dynamicalay in ASPX page. For which i used the XSLT file.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">


    <xsl : output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>

    <xsl:template match="/">
    <table cellSpacing="1" cellPadding="1" border="0">
    <xsl:for-each select="//question">
    <tr>

    <td height="1px"></td>

    </tr>

    <tr>

    <td valign="top" width="100%">


    <xsl:choose>

    <xsl:when test="choice='Yes'">

    <xsl:for-each select="//question">


    </xsl:for-each>


    <xsl:if test="@type='checkbox'">

    <asp:Checkbox runat="server" Cssclass="stylepadding">

    <xsl:for-each select="survey/question/choice/question">

    <xsl:if test="@type='lbl'">

    <asp:Label Cssclass="styletextsublabel1" runat="server"><xsl:value-of select="@name"></xsl:value-of></asp:Label>

    </xsl:if>

    </xsl:for-each>

    </asp:Checkbox>

    </xsl:if>

    </xsl:when>

    </xsl:choose>





    <xsl:if test="@type='label'">

    <asp:Label Cssclass="styletextlabel1" runat="server"> <xsl:value-of select="@name" /></asp:Label>

    </xsl:if>


    <xsl:if test="@type='anchor'">

    <a href="aa.com"><xsl:value-of select="@value" /></a>

    </xsl:if>


    </td>

    </tr>

    <tr>

    <td class="stylepadding">

    <xsl:if test="@type='text'">

    <asp:TextBox id="{@name}" runat="server" />

    </xsl:if>

    <xsl:if test="@type='dropdown'">

    <aspropdownList Cssclass="styletextlabel1" runat="server" Autopostback="true" >

    <xsl:for-each select="choice">

    <asp:ListItem Value="{@value}"><xsl:value-of select="@value" /></asp:ListItem>

    </xsl:for-each>

    </aspropdownList>

    </xsl:if>

    </td>

    </tr>


    <xsl:if test="@type='radio'">

    <asp:RadioButtonList id="{@name}" runat="server">

    <xsl:for-each select="choice">

    <asp:ListItem Value="{@value}"><xsl:value-of select="@value" /></asp:ListItem>

    </xsl:for-each>

    </asp:RadioButtonList>

    </xsl:if>


    </xsl:for-each>

    </table>

    </xsl:template>

    </xsl:stylesheet>

    (ie), I have rendered the questions with the choice 'Yes' and 'No' in a dropdown. Now i need to place a condition, when I choose the value 'Yes' in the dropdown, the other set of questions and choice in the XML file should appear, but this shouldn't happen on select of 'No' option in the dropdown.

    Help me out in resolving this..

    Thanks in advance..

    -----

    how to approach for building dynamic controls in ASP.net by reading from an XML file using XSLT.

    The Dynamic controls should be generated based on conditions in the XML file.

    For example:

    If the tag has an option value 'Yes' dynamic controls generated based on this value will be different. If the option value is 'No', another set of dynamic controls should appear.

    I find only the basic examples.Can any one help me out?
    Last edited by sudha_baskar; 07-04-2006 at 01:17 AM.

  4. #4
    Join Date
    Jun 2006
    Posts
    9
    Sudha,

    I have some concerns with the stylesheet you've listed here. It's difficult to see what the intention is in certain places so I'm attempting to clean it up a bit if you don't mind. For one thing, XSLT is a declarative syntax that falls to pieces when you try to write procedural logic. What I mean to say is that you have to respect the nature of the language to allow it to work for you. In your example it feels like it's working against you.

    Now I'm not sure about this but it seems like you want to do something like this:
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">
        <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
        
        <xsl:template match="/survey">
            <table cellSpacing="1" cellPadding="1" border="0">
                <xsl:apply-templates mode="process" select="question"/>
            </table>
        </xsl:template>
    
        <!-- Process each question nesting it in a cell in a second row -->
        <xsl:template match="question" mode="process">
            <tr>
                <td height="1px"></td>
            </tr>
            <tr>
                <!-- Expand the question template for the current element -->
                <td><xsl:apply-templates select="."/></td>
            </tr>
        </xsl:template>
        
        <xsl:template match="question[@type='lbl' or @type='label']">
            <asp:Label Cssclass="styletextsublabel1" runat="server">
                <xsl:value-of select="@name"></xsl:value-of>
            </asp:Label>
        </xsl:template>
    
        <xsl:template match="question[@type='checkbox']">
            <asp:Checkbox runat="server" Cssclass="stylepadding">
                <!-- recursively process nested question elements -->
                <xsl:apply-templates select="question"/>
            </asp:Checkbox>
        </xsl:template>
    
        <xsl:template match="question[@type='anchor']">
            <a href="aa.com">
                <xsl:value-of select="@value"/>
            </a>
        </xsl:template>
    
        <xsl:template match="question[@type='text']">
            <asp:TextBox id="{@name}" runat="server"/>
        </xsl:template>
    
        <xsl:template match="question[@type='dropdown']">
            <aspropdownList Cssclass="styletextlabel1" runat="server" Autopostback="true">
                <xsl:apply-templates select="choice"/>
            </aspropdownList>
        </xsl:template>
    
        <xsl:template match="question[@type='radio']">
            <asp:RadioButtonList id="{@name}" runat="server">
                <xsl:apply-templates select="choice"/>
            </asp:RadioButtonList>
        </xsl:template>
    
        <xsl:template match="choice">
            <asp:ListItem Value="{@value}">
                <xsl:value-of select="@value"/>
            </asp:ListItem>
        </xsl:template>
    
    </xsl:stylesheet>
    In my example I start my processing at the survey node and begin processing question elements. I use the mode attribute to direct processing to a particular template which starts by writing two table rows and expanding the current node in the second row. You'll see a template defined for each expected type of question and these template create the cooresponding controls. Taking this approach reduces redundancies I found in your if/else logic. (I think I found a bug or typo too, but more on that later). I am also concerned about conditions like this one that I found under a for-each matching all question elements:
    Code:
    <xsl:when test="choice='Yes'">
    That says "when the choice element under the current (question) element has a text value of yes". Given your XML input that condition would never be true. I believe you are looking for the "value" attribute value of the choice element here.

    I have some more questions. You'll have to explain the following syntax:
    Code:
            <asp:TextBox id="{@name}" runat="server"/>
    Are you looking to copy the value of the name attribute from the current element into the id attribute here? I'm not sure it works this way, though the MS processor may exploit some features that support this. I can't be sure. I would code it like this:
    Code:
    <asp:TextBox id="{@name}" runat="server">
       <xsl:attribute name="id"><xsl:value-of select="@name"/></xsl:attribute>
    </asp:TextBox>
    That should give you an ASP textbox control with the id equal to the name attribute of the questions elements of type text. I know the "{}" syntax works in xsl element attributes I'm just not sure if it applies to attributes in any element attribute. There are one or two other occurences of that syntax elsewhere. I'm still trying to get to the part where the condition on the choice elements is a problem. I'm confused still about your processing and need to work out the flow more. In particular, how does the stylesheet know which value was selected from the dropdown? It seems as though either the stylesheet needs to be parameterized or the selected value should be located somewhere in the XML input to be able to make the condition work. Are you saying that selecting "no" from the dropdown would cause a different set of XML input to be processed? Something like this
    Code:
    <question type="dropdown" id="ddlSQ2a">
        <choice value=""></choice>
        <choice value="No" name="No" id="No">
            <question type="lbl" name="Please select one or more sub-domains"></question>
            <question type="checkbox" name="Perform Research and Design"></question>
            <question type="checkbox" name="Create and Manage Product Data"></question>
            <question type="checkbox" name="Manage Product Lifecycle"></question>
        </choice>
        <choice value="Yes"></choice>
    </question>
    ...instead of what you have above? If so then my above should condition the output accordingly.

    Once again, I'm not familiar with ASP.net so if you could answer my above questions regarding the peculiar syntax and processing logic I may be of better assistance. I hope I'm not confusing you more with my response.
    Last edited by Cliff76; 07-06-2006 at 11:24 AM.

  5. #5
    Join Date
    Jul 2006
    Location
    Toronto
    Posts
    2

    Use variables or DOM programming

    XSLT is not the only way to do this, though you could do it using variables.

    My team solves this using DOM code. We use XSLT to create well-formed HTML containing a <FORM>, then we map values to the <FORM> element's child <INPUT> and <SELECT> and <TEXTAREA> fields. This provides a generic way to populate any form.

    Good luck

Similar Threads

  1. QUERY: Mixing ASP.NET 1.1 & 2.0 pages in same website
    By dhruba.bandopad in forum ASP.NET
    Replies: 3
    Last Post: 10-18-2006, 05:32 PM
  2. Replies: 2
    Last Post: 08-01-2005, 04:12 PM
  3. Advantages of ASP.NET over Java/ASP
    By Tricia in forum .NET
    Replies: 0
    Last Post: 04-11-2003, 10:34 PM
  4. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 04:39 AM
  5. Chicago ASP.NET Class May 15-17
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 04-10-2002, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center