Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2008, 01:11 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Access Variable outside for-each

Hi , i have some variable inside for-each , how i can access these variables outside for-each

eg:

<xsl:template name="ValidateRequest">
<xsl:for-each select="../../Check/Node">
    <xsl:if test="@Name = $Name">
        <xsl:for-each select="Type">
            <xsl:if test="@name='MANDATORY'">
                <xsl:variable name="Mandatory" select="@name"/>
            </xsl:if>
            <xsl:if test="@name ='LENGTH'">
                <xsl:variable name="Length" select="@name"/>
            </xsl:if>
            <xsl:if test="@name='CONTENT'">
                <xsl:variable name="Content" select="@name"/>
            </xsl:if>
            <xsl:if test="@name='FORMAT'">
                <xsl:variable name="Format" select="@name"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:if>
</xsl:for-each>

<Request>
    <xsl:element name="Name">
        <xsl:value-of select="$Mandatory"/>
    </xsl:element>
    <xsl:element name="StartPosition">
        <xsl:value-of select="$Length"/>
    </xsl:element>
    <xsl:element name="EndPosition">
        <xsl:value-of select="$Content"/>
    </xsl:element>
    <xsl:element name="XmlNode">
        <xsl:value-of select="$Format"/>
    </xsl:element>
</Request>.
</xsl:template>

--Dishant



 
Old September 25th, 2008, 01:31 AM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can't access these variables outside the for-each but you can define them with a specific XPath expression such as
Code:
<xsl:variable name="Mandatory" select="../../Check/Node[@Name = $Name]/Type/@name[@name = 'MANDATORY']"/>
Actually the value is always 'MANDATORY' if such an attribute exists...

 
Old September 25th, 2008, 02:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want

<xsl:variable name="Length"
   select="../../Check/Node[@Name = 'LENGTH']"/>

and similarly for the other three variables.



Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old September 25th, 2008, 03:03 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
When i am using
           <xsl:if test="@name='MANDATORY'">
                <xsl:variable name="Mandatory" select="@name"/>
            </xsl:if>
I am getting values in Mandatory variable but now i want to use this variable to check some condition , How to to that because when i m trying to access this variable outside for-each then variable is blank.

 
Old September 25th, 2008, 03:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You have to do it the other way around

<xsl:variable name="x">
  <xsl:if test="Y">
    <xsl:value-of select="Z"/>
  </xsl:if>
</xsl:variable>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
access the local variable value out side ravi_sri24 XSLT 17 May 7th, 2008 08:43 AM
How to Access Variable in Templet dishant XSLT 18 November 3rd, 2007 05:40 AM
Global Access to a Variable ? navdeep C# 15 July 19th, 2007 08:44 PM
Populate a Variable From Access Table sirmilt Access 5 March 21st, 2006 01:34 AM
Passing a Variable to a SubForm in Access ritag Access 1 September 29th, 2004 03:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.