Subject: Compare parent's attribute with grand parent's one
Posted By: scubaduba Post Date: 11/29/2004 4:40:25 AM
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>

<!-- Things to be done goes here -->

</xsl:template>

so far I wasn't able to make it work.
Reply By: joefawcett Reply Date: 11/29/2004 6:57:35 AM
I think you need the curent function in your test:

<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)
Reply By: barcher Reply Date: 11/29/2004 11:20:49 AM
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

Reply By: joefawcett Reply Date: 11/29/2004 11:26:42 AM
Too right, I changed horses midstream and forgot it was no longer needed

<?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)

Go to topic 22470

Return to index page 703
Return to index page 702
Return to index page 701
Return to index page 700
Return to index page 699
Return to index page 698
Return to index page 697
Return to index page 696
Return to index page 695
Return to index page 694