CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Posts
    11

    Question on XSLT templates -- urgent

    Given below is the xml and xsl code snippet for my web page.
    I have used two templates one for filling Medium drop down and another for Priority drop down.

    My question is ...

    the template match="Priority/PriorityList" --does this execute for both request call and response call??

    If it executes...for both ...is there any way to write one generic template which serves for all of the 4 template calls??



    XML CODE SNIPPET:
    ----------------
    <Report>
    <CasePacketRequest>
    <ReqPriority></ReqPriority>
    <Priority>
    <PriorityList value="E">Expedited</PriorityList>
    <PriorityList value="S">Standard</PriorityList>
    </Priority>
    <ReqMedium></ReqMedium>
    <Medium>
    <MediumList value="F">Fax</MediumList>
    <MediumList value="M">Mail</MediumList>
    </Medium>
    </CasePacketRequest>
    <CasePacketResponse>
    <ResPriority></ResPriority>
    <Priority>
    <PriorityList value="E">Expedited</PriorityList>
    <PriorityList value="S">Standard</PriorityList>
    </Priority>
    <ResMedium></ResMedium>
    <Medium>
    <MediumList value="F">Fax</MediumList>
    <MediumList value="M">Mail</MediumList>
    </Medium>
    </CasePacketResponse>
    </Report>


    XSL CODE SNIPPET:
    ----------------
    <select name="selectPriority" class="bodytext" onChange="changeOption(selectedIndex)">
    <xsl:apply-templates select="CasePacketRequest/Priority/PriorityList"/>
    </select>

    <select name="selectMedium" class="bodytext" onChange="changeOption(selectedIndex)">
    <option value="">--- Select ---</option>
    <xsl:apply-templates select="CasePacketRequest/Medium/MediumList"/>
    </select>

    <select name="selectPriority" class="bodytext" onChange="changeOption(selectedIndex)">
    <xsl:apply-templates select="CasePacketResponse/Priority/PriorityList"/>
    </select>

    <select name="selectMedium" class="bodytext" onChange="changeOption(selectedIndex)">
    <option value="">--- Select ---</option>
    <xsl:apply-templates select="CasePacketResponse/Medium/MediumList"/>
    </select>


    <xsl:template match="Priority/PriorityList">
    <option><xsl:attribute name="value"><xsl:value-of select="@value"/>
    </xsl:attribute>
    <xsl:if test="../ReqPriority=@value">
    <xsl:attribute name="selected" />
    </xsl:if>
    <xsl:value-of select="."/>
    </option>
    </xsl:template >

    <xsl:template match="Medium/MediumList">
    <option><xsl:attribute name="value"><xsl:value-of select="@value"/>
    </xsl:attribute>
    <xsl:if test="../ReqMedium=@value">
    <xsl:attribute name="selected" />
    </xsl:if>
    <xsl:value-of select="."/>
    </option>
    </xsl:template >

  2. #2
    Join Date
    Oct 2004
    Posts
    107

    Re: Question on XSLT templates -- urgent

    1. You can shorten the name of the templates to only the name of the node, eg:
    <xsl:template match="PriorityList">
    This should work as long as you don't have any other PriorityList nodes anywhere else.

    2. Your <xsl:if> test clauses are off by one level, as we are in the PriorityList or MediumList node, not it's parent. It should be:
    <xsl:if test="@value = ../../ReqPriority">

    3. You can merge the 2; the <xsl:if> test would look something like:
    <xsl:if test="(name() = 'PriorityList' and @value = ../../ReqPriority) or
    (name() = 'MediumList' and @value = ../../ReqMedium)">

    4. Use the union | operator to merge the templates. So in total:
    <xsl:template match="PriorityList|MediumList">
    <option><xsl:attribute name="value"><xsl:value-of select="@value"/>
    </xsl:attribute>
    <xsl:if test="(name() = 'PriorityList' and @value = ../../ReqPriority) or
    (name() = 'MediumList' and @value = ../../ReqMedium)">
    <xsl:attribute name="selected" />
    </xsl:if>
    <xsl:value-of select="."/>
    </option>
    </xsl:template >

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured