Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 5th, 2006, 07:32 AM
Authorized User
 
Join Date: Jun 2006
Location: Sydney, NSW, Australia.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding parent node

Hi there,

below is an example of my source xml.

<root>
    <secondary relatedID="1">
        <a a4="apple" a5="pear"/>
    </secondary>
    <main id="1">
        <fruit>
            <a a1="orange" a2="grape"/>
            <a a1="lime" a3="lemon"/>
        </fruit>
    </main>
</root>

My desired result should be like

<root>
    <main id="1">
        <fruit>
            <a a1="orange" a2="grape" a4="apple" a5="pear"/>
            <a a1="lime" a3="lemon" a4="apple" a5="pear"/>
        </fruit>
    </main>
</root>

The following is my XSLT. Could someone kindly pls point out where I am goin wrong? Thanks

<xsl:template match="main">
        <main>
            <xsl:for-each select="fruit">
                <fruits>
                    <xsl:apply-templates select="a"/>
                </fruits>
            </xsl:for-each>
        </main>
    </xsl:template>
    <xsl:template match="a">
        <gg>
            <xsl:copy-of select="@*"/>
            <xsl:copy-of select="/../secondary[@relatedID=current()/@id]/a/@*"/>
        </gg>
    </xsl:template>
    <xsl:template match="@*|node()|comment()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()|comment()"/>
        </xsl:copy>
    </xsl:template>


Reply With Quote
  #2 (permalink)  
Old August 6th, 2006, 03:13 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

/.. (or any path beginning /..) selects nothing. That's because "/" selects the root node (that is, the document node, the parent of the node which you have confusingly named "root"), and .. selects its parent, and the root node by definition has no parent.

Try /root/secondary[@relatedID=current()/@id]/a/@*"/>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old August 6th, 2006, 11:25 PM
Authorized User
 
Join Date: Jun 2006
Location: Sydney, NSW, Australia.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for the pointer but it still doesnt seem to be matching.

I even tried using the axes of ancestor and ancestor-or-self
<xsl:copy-of select="/root/secondary [@relatedID=ancestor::main/@id]/a/@*"/>

but it doesnt work.

I created two attributes to see if my expression equates. The result of both attribute matches but I dont seem to see the problem why it doesnt copy the attributes from secondary to main. Your feedback is much appreciated. thanks

<xsl:attribute name="secondaryID"><xsl:value-of select="/root/secondary/@relatedID"/></xsl:attribute>

<xsl:attribute name="MainID"><xsl:value-of select="ancestor::main/@id"/></xsl:attribute>

Reply With Quote
  #4 (permalink)  
Old August 7th, 2006, 11:52 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see what's wrong from this snippet, sorry.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old August 9th, 2006, 06:26 AM
Authorized User
 
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Chamkaur
 Hi Michael,

Thanks for the pointer but it still doesnt seem to be matching.

I even tried using the axes of ancestor and ancestor-or-self
<xsl:copy-of select="/root/secondary [@relatedID=ancestor::main/@id]/a/@*"/>

but it doesnt work.

I created two attributes to see if my expression equates. The result of both attribute matches but I dont seem to see the problem why it doesnt copy the attributes from secondary to main. Your feedback is much appreciated. thanks

<xsl:attribute name="secondaryID"><xsl:value-of select="/root/secondary/@relatedID"/></xsl:attribute>

<xsl:attribute name="MainID"><xsl:value-of select="ancestor::main/@id"/></xsl:attribute>

Because the current() node is the a element and not the main element the id will not be found, also you don't pass the id attribute on your main element still, so also <xsl:copy-of select="@*"/> pass that after <main>
when you add ../../ to your path like this: /root/secondary[@relatedID=current()/../../@id]/a/@*


you will get:
<root>
    <secondary relatedID="1">
        <a a4="apple" a5="pear"/>
    </secondary>
    <main id="1"><fruits><gg a1="orange" a2="grape" a4="apple" a5="pear"/><gg a1="lime" a3="lemon" a4="apple" a5="pear"/></fruits></main>
</root>

is that what you want?
you will have to filter out the secondary element if you don't want that, because now the element will be copied through the template <xsl:template match="@*|node()|comment()"> for instance by adding <xsl:template match="secondary"/>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding if a node value doesn't exist mphare XSLT 3 August 19th, 2007 05:42 PM
retrieving information from a parent node Tomi XSLT 2 September 6th, 2006 06:54 AM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 05:10 AM
finding the most recent node... clayman58 XSLT 5 July 21st, 2005 01:41 PM
Saving the parent node Doyle Bradely XML 2 December 23rd, 2003 07:05 PM



All times are GMT -4. The time now is 03:08 AM.


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