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 November 29th, 2004, 05:40 AM
Authorized User
 
Join Date: Nov 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compare parent's attribute with grand parent's one

Hi,

I wanted to compare an element's parent's name attribute with it's grand parent's name attribute. If they match I wanted to print something. The XPath query I used is this, but apparently it didn't work:

<xsl:when test="..[@name]=../..[@name]">
Edit: I see now that it is a wrong syntax.

Here is my XML structure:
<RootElement>
 <Groups name='top1'>
  <Group name='sub1'>
    <Groups name='sub1'>
        <Group name="sub1ofsub1"/>
        <Group name="sub2ofsub1"/>
        <Group name="sub3ofsub1"/>
    </Groups>
  <Group>
 <Groups>
</RootElement>

I can achieve my goal with defining variables for name attributes and comparing them, but if there is a Xpath structure I can use I'd prefere one line code rather than variables.

Thanks in advance.

EDIT: I thought it would be easy to do what I want with variables but I wasn't even able to do that. There is no runtime variable assignment in XSL, which makes things harder.
 Here is the XSL structure I tried to get values ( I assume I'm in one of the inner Group elements):

<xsl:template match="Group">
 <xsl:variable name="parentsname">
   <xsl:value-of select="..[name()='Groups']/[@name]"/>
 </xsl:variable>

 <xsl:variable name="grandparentsname">
  <xsl:value-of select="..[name()='Groups']/..[name()='Group']/[@name]"/>
 </xsl:variable>



</xsl:template>

so far I wasn't able to make it work.
 
Old November 29th, 2004, 07:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think you need the curent function in your test:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml"/>
<xsl:template match="/">
  <results>
    <xsl:apply-templates/>
  </results>
</xsl:template>

<xsl:template match="*">
  <xsl:value-of select="name()"/><xsl:text>#x9;</xsl:text>
  <xsl:value-of select="../@name"/><xsl:text>#x9;</xsl:text>
  <xsl:value-of select="../../@name"/><xsl:text>#xd;</xsl:text>
  <xsl:choose>
    <xsl:when test="../@name = current()/../../@name">Parent @ name is equal to grandparent @ name</xsl:when>
    <xsl:otherwise>Parent @ name is not equal to grandparent @ name</xsl:otherwise>
  </xsl:choose>
  <xsl:text>#xd;*****************************#xd;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old November 29th, 2004, 12:20 PM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

current() should not be required as both sides of the test will use the current context anyway.

<xsl:if test="../@name=../../@name">using shorthand</xsl:if>
<xsl:if test="parent::*/@name=ancestor::*[2]/@name">using axis</xsl:if>

Regards
Bryan

 
Old November 29th, 2004, 12:26 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Too right, I changed horses midstream and forgot it was no longer needed :)
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml"/>
<xsl:template match="/">
  <results>
    <xsl:apply-templates/>
  </results>
</xsl:template>

<xsl:template match="*">
  <xsl:value-of select="name()"/><xsl:text>#x9;</xsl:text>
  <xsl:value-of select="../@name"/><xsl:text>#x9;</xsl:text>
  <xsl:value-of select="../../@name"/><xsl:text>#xd;</xsl:text>
  <xsl:choose>
    <xsl:when test="../@name = ../../@name">Parent @name is equal to grandparent @name</xsl:when>
    <xsl:otherwise>Parent @name is not equal to grandparent @name</xsl:otherwise>
  </xsl:choose>
  <xsl:text>#xd;*****************************#xd;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
calculating grand total blkskullwork Javascript 2 December 18th, 2006 10:36 AM
compare these date fields and compare and get the susanring Oracle 1 July 24th, 2006 04:58 PM
PIVOT TABLE Grand Total ... pls help !!!! laky Access VBA 0 June 2nd, 2006 03:47 AM
Grand Total mateenmohd Access 6 April 8th, 2005 07:30 AM
Forcing a form in the parent's parent to submit Snib Javascript 11 June 22nd, 2004 11:48 PM





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