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 October 26th, 2007, 07:12 AM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Access Variable in Templet

 Hey I m trying to Access a Variable from template i m not Abel to do it , please can any one tell me how to do that

i doing in this way :



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="Append">
        <xsl:value-of select="'~'"/>
    </xsl:variable>
    <xsl:variable name="Status">
        <xsl:value-of select="0"/>
    </xsl:variable>
    <xsl:element name="INFORMATION">
        <xsl:choose>
            <xsl:when test="DATA[MessageName='Verify']">
                <xsl:call-template name="Inquiry">
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="DATA[MessageName='Verify1']">
                <xsl:call-template name="Inquiry1">
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:element>
    <xsl:template name="Inquiry">
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$Status"/>
    </xsl:template>
    <xsl:template name="Inquiry1">
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$Status"/>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>


 
Old October 26th, 2007, 07:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The variables look fine, but the <xsl:element> isn't a top level declaration, so should be inside something else, like a <xsl:template> declaration.

/- Sam Judson : Wrox Technical Editor -/
 
Old October 26th, 2007, 08:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't do this:

<xsl:variable name="Append">
   <xsl:value-of select="'~'"/>
</xsl:variable>

do this instead:

<xsl:variable name="Append" select="'~'"/>

It's one line of code instead of 3, and it's much more efficient because the variable is a simple string rather than an XML document.

XSLT variables have static scope, like variables in C or Java - you can't access them outside the template where they were declared. If you want to pass them to another template, you need parameters - look up xsl:with-param and xsl:param, any XSLT book will give you examples of their use.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 26th, 2007, 08:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>but the <xsl:element> isn't a top level declaration

Sorry, I missed that. Because the variables were on the same level as xsl:element, I assumed they were local variables within a template.

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

Thanx Michael Kay

 But My Problem is that i want all the Data in one sting separated by " ~ " .

so thats why i m taking node one by one without <xsl:element> because if i use <xsl:element> then i will get value in numbers of node but i want them in one node

so can u give me sum examples how to do that


 
Old October 26th, 2007, 10:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Quote:
quote:Originally posted by dishant
so thats why i m taking node one by one without <xsl:element> because if i use <xsl:element> then i will get value in numbers of node but i want them in one node
You ARE using <xsl:element> but you shouldn't be, at least not there. I suspect it should read: <xsl:template match="INFORMATION"> etc.

Even if the above was a correct XSLT stylesheet it wouldn't output anything apart from a '~' and the number 0.

You probably want to do something with <xsl:for-each> but its hard to say what without seeing a sample of your input XML (a small sample will do) and what you want the output to look like.

Just off the top of the head think along these lines...

Code:
<xsl:for-each select="DATA">
  <xsl:value-of select="string(.)"/>
  <xsl:if test="position != last()">
    <xsl:value-of select="'~'"/>
  </xsl:if>
<xsl:for-each>
/- Sam Judson : Wrox Technical Editor -/
 
Old October 26th, 2007, 11:36 PM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi samjudson,

right now i m not giving any input i m just trying it with harcoded value but problem is that when i m trying this


<xsl:template name="Inquiry">
   <xsl:value-of select="$Append"/>
   <xsl:value-of select="$Status"/>
   <xsl:value-of select="$Append"/>
   <xsl:value-of select="$Status"/>
</xsl:template>


and right now i m not taking any value form XML thats y i m not using <xsl:template match="INFORMATION">
I am only getting value for Append [ie ~ ] but i want value in string i.e [ ~0~0 ] and i m not getting that . so could u plz tell me what is the way to get that


and if i do about exampel in other vay way i.e

<xsl:template name="Inquiry">
    <xsl:element name="Append">
       <xsl:value-of select="$Append"/>
    </xsl:element>
    <xsl:element name="ProcessingStatus">
       <xsl:value-of select="$Status"/>
    </xsl:element>
</xsl:template>

it is woring but i want that in string not in node

 
Old October 26th, 2007, 11:38 PM
Authorized User
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi samjudson,

right now i m not giving any input i m just trying it with harcoded value but problem is that when i m trying this


<xsl:template name="Inquiry">
   <xsl:value-of select="$Append"/>
   <xsl:value-of select="$Status"/>
   <xsl:value-of select="$Append"/>
   <xsl:value-of select="$Status"/>
</xsl:template>



I am only getting value for Append [ie ~ ] but i want value in string i.e [ ~0~0 ] and i m not getting that . so could u plz tell me what is the way to get that

and right now i m not taking any value form XML thats y i m not using <xsl:template match="INFORMATION">

and if i do about exampel in other vay way i.e

<xsl:template name="Inquiry">
    <xsl:element name="Append">
       <xsl:value-of select="$Append"/>
    </xsl:element>
    <xsl:element name="ProcessingStatus">
       <xsl:value-of select="$Status"/>
    </xsl:element>
</xsl:template>

it is woring but i want that in string not in node

 
Old October 27th, 2007, 03:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please don't use text shorthand. It takes twice as long to read, and I'm a busy person.

I think it would help if you explain exactly (and completely) what your source document and stylesheet look like, what output you are getting, and what output you want.

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

Hi Michael Kay
  I m just trying to redus the code , but following is my XSL , below that there is Input OutPut log file i M checking Message name from there if is satisfy i want to save my data there



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="Append">
        <xsl:value-of select="'~'"/>
    </xsl:variable>
    <xsl:variable name="Status">
        <xsl:value-of select="0"/>
    </xsl:variable>
    <xsl:variable name="Name">
            <xsl:value-of select="ABC"/>
    </xsl:variable>
    <xsl:variable name="LastName">
            <xsl:value-of select="XYZ"/>
    </xsl:variable>
    <xsl:template match="DATA">
        <xsl:element name="INFORMATION">
        <xsl:choose>
            <xsl:when test="[MessageName='Verify']">
            <xsl:call-template name="Inquiry">
            </xsl:call-template>
            </xsl:when>
            <xsl:when test="[MessageName='Verify1']">
            <xsl:call-template name="Inquiry1">
            </xsl:call-template>
            </xsl:when>
        </xsl:choose>
        </xsl:element>
    </xsl:template>
    <xsl:template name="Inquiry">
        <xsl:value-of select="$Status"/>
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$Name"/>
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$LastName"/>
    </xsl:template>
    <xsl:template name="Inquiry1">
        <xsl:value-of select="$Status"/>
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$Name"/>
        <xsl:value-of select="$Append"/>
        <xsl:value-of select="$LastName"/>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>



*************************************************
I want my out put should be <INFORMATION>0~ABC~XYZ</INFORMATION> but i not getting .


Input / OutPut:

<context>
    <DATA>
        <MessageName>Verify</MessageName>
    </DATA>
    <INFORMATION>0~ABC~XYZ</INFORMATION>
</context>

Hope it is sufficient i have written all what i have:)







Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Access Variable outside for-each dishant XSLT 4 September 25th, 2008 03:53 AM
access the local variable value out side ravi_sri24 XSLT 17 May 7th, 2008 08:43 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.