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 April 1st, 2008, 04:59 AM
Authorized User
 
Join Date: Jan 2008
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default access the local variable value out side

please consider the below code..

<xsl:if test="$HD='OH'">
    <xsl:variable name="variable1" select="ABC" />
</xsl:if>

How to access the value of "variable1"(which is inside the if block)out side the if block.or please give me the alternative solution for the above



 
Old April 1st, 2008, 05:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't do that because once outside of the <xsl:if> the variable is no longer in scope.

You can place the <xsl:if> inside the variable however:

<xsl:variable name="variable1">
  <xsl:if test="$HD='OH'>ABC</xsl:if>
</xsl:variable>

or in XSLT 2.0 inline:

<xsl:variable name="variable1" select="if ($HD='OH') then 'ABC' else ''"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old April 1st, 2008, 07:46 AM
Authorized User
 
Join Date: Jan 2008
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Sam,

But here my case is different

am getting the CSV file in that first record is always Header and the remaining are the actual records

i would like to generate a XML file by taking the actual record value's but in while generating the xml i need to generate the few tag's by taking the data from the header also

so that's the reason why i want to store header information in global varible so that i can use when ever required


Please find the my XSLT


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" version="2.0">
<xsl:output method="xml"/>

<xsl:variable name="varRecordBreak" select="'#10;'"/>
<xsl:variable name="varFieldDelimiter" select="','" />
<xsl:variable name="h1" />
<xsl:variable name="h2" />
<xsl:variable name="h3" />

<xsl:template match="/">
    <xsl:for-each select="datastream/payload">
            <xsl:call-template name="payload"/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="payload">


               <xsl:for-each select="str:tokenize(., '#10;')">
                <xsl:variable name="header">
                    <xsl:if test="position() =1">
                        <xsl:value-of select="."/>
                    </xsl:if>
                </xsl:variable>
                <xsl:variable name="details">
                    <xsl:if test="position() >1">
                        <xsl:value-of select="."/>
                    </xsl:if>
                </xsl:variable>
                <xsl:variable name="varHeader" select="java:java.util.StringTokenizer.new(java:ja va.lang.String.new(normalize-space($header)),$varFieldDelimiter)" />
                <xsl:variable name="varDetails" select="java:java.util.StringTokenizer.new(java:ja va.lang.String.new(normalize-space($details)),$varFieldDelimiter)" />

                <xsl:variable name="h1" select="java:nextToken($varHeader)" />
                <xsl:variable name="h2" select="java:nextToken($varHeader)" />
                <xsl:variable name="h3" select="java:nextToken($varHeader)" />
                <xsl:variable name="d1" select="java:nextToken($varDetails)" />
                <xsl:variable name="d2" select="java:nextToken($varDetails)" />
                <xsl:variable name="d3" select="java:nextToken($varDetails)" />
                <Records>
                    <h1><xsl:value-of select="$h1" /></h1>
                    <h2><xsl:value-of select="$h2" /></h2>
                    <h3><xsl:value-of select="$h3" /></h3>
                    <d1><xsl:value-of select="$d1" /></d1>
                    <d2><xsl:value-of select="$d2" /></d2>
                    <d3><xsl:value-of select="$d3" /></d3>
                </Records>



                  </xsl:for-each>

</xsl:template>
</xsl:stylesheet>




 
Old April 1st, 2008, 09:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're still thinking very procedurally; you need to learn to think in a more declarative, functional style. You really don't want to use calls to Java methods such as nextToken() that depend on the order of execution. In fact this kind of thing is a lot easier in XSLT 2.0 where you don't need calls to extension functions at all.

The XSLT way to do this is something like this:

<xsl:variable name="tokens" select="str:tokenize(...)"/>
<xsl:variable name="h1" select="$tokens[1]"/>
<xsl:variable name="h2" select="$tokens[2]"/>

etc.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 2nd, 2008, 12:50 AM
Authorized User
 
Join Date: Jan 2008
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Kay...m not using XSLT 2.0 version...can u plz help me for XSLT ver 1.0

 
Old April 2nd, 2008, 01:21 AM
Authorized User
 
Join Date: Jan 2008
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for the solution,

But here the problem is am using the Adapter developed by my client that will support Xalan Parser(xalan parser supports only XSLT1.0)

So i can't go for the XSLT2.0, Please give any solution by using XSLT1.0

is it possible to do with the exslt name space?



 
Old April 2nd, 2008, 03:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Michael's code example above is XSLT 1.0.

If you are already using the str:tokenize function then there is little point in doing the same thing but calling out to java, as its not needed.

Code:
<xsl:variable name="tokens" select="str:tokenize(., '#10;')"/>
<xsl:variable name="headers" select="str:tokenize(normalize-space($tokens[1])),$varFieldDelimiter)"/>

<xsl:for-each select="$tokens">
  <xsl:if test="position() > 1 ">
    <xsl:variable name="details" select="str:tokenize(normalize-space(.)),$varFieldDelimiter)"/>

<Records>
  <h1><xsl:value-of select="$headers[1]" /></h1>
  <h2><xsl:value-of select="$headers[2]" /></h2>
  <h3><xsl:value-of select="$headers[3]" /></h3>
  <d1><xsl:value-of select="$details[1]" /></d1>
  <d2><xsl:value-of select="$details[2]" /></d2>
  <d3><xsl:value-of select="$details[3]" /></d3>
</Records>

  </xsl:if>
</xsl:for-each>

/- Sam Judson : Wrox Technical Editor -/
 
Old April 2nd, 2008, 10:07 AM
Authorized User
 
Join Date: Jan 2008
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Sam

I have modified the code according to your suggestion...but i am getting the following error:

Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
    at org.apache.xpath.XPath.execute(Unknown Source)
    at org.apache.xalan.templates.ElemVariable.getValue(U nknown Source)
    at org.apache.xalan.templates.ElemVariable.execute(Un known Source)
    at org.apache.xalan.templates.ElemApplyTemplates.tran sformSelectedNodes(Unknown Source)
    at org.apache.xalan.templates.ElemApplyTemplates.exec ute(Unknown Source)
    at org.apache.xalan.templates.ElemApplyTemplates.tran sformSelectedNodes(Unknown Source)
    at org.apache.xalan.templates.ElemApplyTemplates.exec ute(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.execu teChildTemplates(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.apply TemplateToNode(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.trans formNode(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.trans form(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.trans form(Unknown Source)
    at org.apache.xalan.transformer.TransformerImpl.trans form(Unknown Source)
    at com.gsk.gskgaa.utils.MessageUtil.processXML(Messag eUtil.java:558)
    ... 6 more
Caused by: java.lang.ClassCastException
    at org.apache.xpath.axes.FilterExprIteratorSimple.exe cuteFilterExpr(Unknown Source)
    at org.apache.xpath.axes.FilterExprWalker.setRoot(Unk nown Source)
    at org.apache.xpath.axes.WalkingIterator.setRoot(Unkn own Source)
    at org.apache.xpath.axes.NodeSequence.setRoot(Unknown Source)
    at org.apache.xpath.axes.LocPathIterator.execute(Unkn own Source)

Herewith is the code below:

<xsl:stylesheet xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" exclude-result-prefixes="java" version="2.0" >

<xsl:output omit-xml-declaration="yes" />

<xsl:variable name="varPayload" select="datastream/payload" />
<xsl:variable name="varRecordBreak" select="'#x0A;'"/>
<xsl:variable name="varFieldDelimiter" select="','" />

<xsl:template match="/datastream/payload">

<xsl:variable name="tokens" select="str:tokenize(., '#x0A;')"/>
<xsl:variable name="headers" select="java:java.util.StringTokenizer.new($tokens[1],$varFieldDelimiter)" />
    <xsl:variable name="varProprietaryDocumentIdentifier" select="$headers[2]" />
                  <xsl:variable name="varRevisionNumber" select="$headers[3]" />
                  <xsl:variable name="varGlobalBusinessIdentifier_Sold" select="$headers[4]" />
    <xsl:variable name="varGlobalBusinessIdentifier_Bill" select="$headers[5]" />
                  <xsl:variable name="varGlobalBusinessIdentifier_Ship" select="$headers[6]" />
    <xsl:variable name="varAccountNumber" select="$headers[7]" />
                  <xsl:variable name="varDateTimeStamp" select="$headers" />
                  <xsl:variable name="varGlobalDocumentFunctionCode" select="$headers[9]" />

<xsl:for-each select="$tokens">
  <xsl:if test="position() > 1 ">
    <xsl:variable name="details" select="java:java.util.StringTokenizer.new(.,$varF ieldDelimiter)" />


                  <xsl:variable name="varLineNumber" select="$details[3]" />
                  <xsl:variable name="varGlobalProductIdentifier" select="$details[6]" />
                 <xsl:variable name="varDateStamp" select="$details[7]" />
                  <xsl:variable name="varProductQuantity" select="$details" />
                  <xsl:variable name="varGlobalProductUnitOfMeasureCode" select="$details[9]" />
     <xsl:variable name="varMonetaryAmount_GROSS" select="$details[10]" />
                  <xsl:variable name="varMonetaryAmount_NETT" select="$details[11]" />
                   <xsl:variable name="varGlobalDocumentReferenceTypeCode" select="$details[12]" />

<GlobalDocumentFunctionCode><xsl:value-of select="$varGlobalDocumentFunctionCode" /></GlobalDocumentFunctionCode>
<AccountNumber><xsl:value-of select="$varAccountNumber" /></AccountNumber>
<GlobalBusinessIdentifier><xsl:value-of select="$varGlobalBusinessIdentifier_Bill" /></GlobalBusinessIdentifier>
<DateTimeStamp><xsl:value-of select="$varDateTimeStamp" /></DateTimeStamp>
<ProprietaryDocumentIdentifier><xsl:value-of select="$varProprietaryDocumentIdentifier" /></ProprietaryDocumentIdentifier>
<RevisionNumber><xsl:value-of select="$varRevisionNumber" /></RevisionNumber>
<GlobalDocumentReferenceTypeCode><xsl:value-of select="$varGlobalDocumentReferenceTypeCode" /></GlobalDocumentReferenceTypeCode>
<GlobalProductUnitOfMeasureCode><xsl:value-of select="$varGlobalProductUnitOfMeasureCode" /></GlobalProductUnitOfMeasureCode>
<LineNumber><xsl:value-of select="$varLineNumber" /></LineNumber>
<ProductQuantity><xsl:value-of select="$varProductQuantity" /></ProductQuantity>
<GlobalProductIdentifier><xsl:value-of select="$varGlobalProductIdentifier" /></GlobalProductIdentifier>
<DateStamp><xsl:value-of select="$varDateStamp" /></DateStamp>
<xsl:value-of select="$varMonetaryAmount_GROSS" />
<xsl:value-of select="$varMonetaryAmount_NETT" />
<GlobalBusinessIdentifier><xsl:value-of select="$varGlobalBusinessIdentifier_Ship" /></GlobalBusinessIdentifier>
<GlobalBusinessIdentifier><xsl:value-of select="$varGlobalBusinessIdentifier_Sold" /></GlobalBusinessIdentifier>

 </xsl:if>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

 
Old April 2nd, 2008, 10:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's crashing in Xalan, presumably when calling the extension function. You will have to go to the Xalan support forum for this, or use a different product.

Really there is not much point in using XSLT 1.0 with vendor extensions when you could write this so easily in XSLT 2.0.

You didn't follow Sam's suggestions anyway. There's no point coming here for help if you won't take our advice.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 2nd, 2008, 10:49 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

My suggestion was to not use the Java Tokenizer at all, yet you are still trying to use it.

You are then trying to access the Tokenizer like it is an array, which given the fact you where accessing it correctly in the first post you should know better.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Client-side variable feffe General .NET 0 May 9th, 2006 03:35 AM
Returning primary key into asp local variable! jowjow Oracle ASP 0 April 5th, 2006 09:06 AM
How to store ntext or text value to local variable ramk_1978 SQL Server 2000 3 September 14th, 2005 07:59 PM
sharing a server-side variable with client-side pigtail Javascript How-To 6 November 4th, 2004 02:01 AM





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