[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Predicates using current node-set

Subject: RE: Predicates using current node-set
From: Jarno.Elovirta@xxxxxxxxx
Date: Wed, 2 Oct 2002 12:52:09 +0300
transform xsl maintain xsl
Hi,

> I have a problem using predicate expressions where I compare 
> an attribute in
> the node-set to an attribute in the "current node-set". I 
> have problems
> explaing this, so here's an example:
> 
> xml file:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <?xml-stylesheet type="text/xsl" href="paytest.xsl"?>
> <payments>
> 	<oldpayments>
> 		<oldpayment payno="1" amount="100" ref="a" />
> 		<oldpayment payno="2" amount="200" ref="b" />
> 		<oldpayment payno="3" amount="300" ref="c" />
> 	</oldpayments>
> 	<newpayments>
> 		<newpayment Payno="3" amount="300" />
> 		<newpayment Payno="4" amount="300" />
> 	</newpayments>
> </payments>
> 
> xsl file:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="html"/>
> 
> <xsl:template match="/">
> <html>
> <head><title>test</title></head>
> <body>
> <xsl:apply-templates select="/payments/newpayments/newpayment" />
> </body>
> </html>
> </xsl:template>
> 
> <xsl:template match="/payments/newpayments/newpayment">
> 	Payno: <xsl:value-of select="@Payno"/> 
> 	Amount: <xsl:value-of select="@amount"/>
> 	<xsl:if test="/payments/oldpayments/oldpayment/@payno = @Payno">
> 		Ref: <xsl:value-of
> select="/payments/oldpayments/oldpayment[@payno=@Payno]/@ref"/>
> 	</xsl:if>
> 	<br/>
> </xsl:template>
> 
> Result in browser:
> Payno: 3 Amount: 300 Ref: 
> Payno: 4 Amount: 300
> 
> I wanted the "ref" attribute to be printed, but I cant work 
> out how to match
> the oldpayment payno with the newpayment Payno

keys might make your stylesheet more readable and easier to maintain,

<xsl:key name="old" match="oldpayments/oldpayment" use="@payno" />

<xsl:template match="newpayment">
	Payno: <xsl:value-of select="@Payno"/> 
	Amount: <xsl:value-of select="@amount"/>
	<xsl:if test="key('old', @Payno)">
		Ref: <xsl:value-of select="key('old', @Payno)/@ref"/>
	</xsl:if>
	<br/>
</xsl:template>

But as Oleg said, using current() will get you there, too.

Cheers,

Jarno

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.