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 31st, 2003, 08:07 PM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to do it in xslt?


I have a xml file looks like this:

<root>
     <L1>a</L1>
     <L2>b
         <L3>c</L3>
     </L2>
</root>

In my xslt file, I do a template match to get to <L3>

<xsl:template match="L2/L3">
need to use the value of L1.
</xsl:template>

Assuming that I get to the node of L3. Now, my question is, how do I get the value of L1 inside of the that match template? Is there a global variable or something in xslt so I can store the L1's value in that global variable and use it in the match template?

Can anyone tell me how I can do this in xslt? Please give me some details because I am still learning this stuff :-)

Thanks in advance.
 
Old November 1st, 2003, 04:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well it's not clear how you wish to access it, there are a number of possibilities, these are shown together with the xpath which would retrieve it from within the template matching L2/L3:

1) As the first child of the document element:
  (/*/*)[1]
2) As the first preceding sibling of the parent:
  ../preceding-sibling::*[1]
3) As the preceding element:
  (preceding::*)[1]

Here's a stylesheet to see:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8" />
  <xsl:template match="/">
    <xsl:apply-templates select="root/L2/L3"/>
  </xsl:template>
  <xsl:template match="L2/L3">
    (/*/*)[1] gives<xsl:text>#x0a;</xsl:text>
    <xsl:value-of select="(/*/*)[1]"/><xsl:text>#x0a;</xsl:text><xsl:text>#x0a;</xsl:text>
      ../preceding-sibling::*[1] gives<xsl:text>#x0a;</xsl:text>
    <xsl:value-of select="../preceding-sibling::*[1]"/><xsl:text>#x0a;</xsl:text><xsl:text>#x0a;</xsl:text>
    (preceding::*)[1] gives<xsl:text>#x0a;</xsl:text>
    <xsl:value-of select="(preceding::*)[1]"/><xsl:text>#x0a;</xsl:text><xsl:text>#x0a;</xsl:text>    
</xsl:template>
</xsl:stylesheet>

Joe (MVP - xml)
 
Old November 1st, 2003, 05:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

In any case you must use an XPath expression to get the node you want:

a) using a variable:
declare this variable before all templates
Code:
  <xsl:variable name="l1" select="/root/L1"/>
then use the variable in the template body:

Code:
  <xsl:value-of select="$l1"/>
b) or simply inside the template body write:

Code:
  <xsl:value-of select="/root/L1"/>
c) or you can get the node "L1" from the current node(L2/L3) in the template body:

Code:
  <xsl:value-of select="../preceding-sibling::L1"/>

Regards,
Armen
 
Old November 1st, 2003, 06:30 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

There u can use <xsl:value-of select"."/> tag this will print the text between the L3 Tag.

bye
Pulavarthi




Quote:
quote:Originally posted by newtoxml


I have a xml file looks like this:

<root>
     <L1>a</L1>
     <L2>b
         <L3>c</L3>
     </L2>
</root>

In my xslt file, I do a template match to get to <L3>

<xsl:template match="L2/L3">
need to use the value of L1.
</xsl:template>

Assuming that I get to the node of L3. Now, my question is, how do I get the value of L1 inside of the that match template? Is there a global variable or something in xslt so I can store the L1's value in that global variable and use it in the match template?

Can anyone tell me how I can do this in xslt? Please give me some details because I am still learning this stuff :-)

Thanks in advance.










Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XSLT with XSLT stonis XSLT 3 April 1st, 2008 08:17 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xslt with an xslt outputfile alleycat XSLT 4 February 20th, 2006 09:56 AM





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