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 April 24th, 2009, 10:31 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default xsl:key get value of ancestor

Hi, If I have a key:

Code:
    <xsl:key name="section-key" match="sec1" use="descendant::*/@id"/>
which will create a key index of all its descendants that have a @id attribute.

Then I want to look up the value of like so:

Code:
<xsl:value-of select="key('section-key', @href)/ancestor::sec1/title"/>
is this possible?

Thanks

Last edited by bonekrusher; April 24th, 2009 at 10:46 AM..
 
Old April 24th, 2009, 11:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The key() function will return a sec1 element so the use of ancestor::sec1 looks wrong.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 24th, 2009, 11:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Your select expression is certainly possible.
However, with your key definition calling the key function with key('section-key', @href) will give you a node-set of 'sec1' element already so I am not sure you want the ancestor::sec1.
You might want to post a sample of your XML and explain what you are looking for.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 24th, 2009, 11:08 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks Michael,

Does this mean I can't find an ancestor value via the key function or my syntax is incorrect?

Here is some sample code:

input:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <chapter id="chapter1">
        <sec1>
            <para id="para1">This is a para1</para>
        </sec1>
    </chapter>
    <chapter id="a2">
        <sec1 id="chap2sec1">
            <title>Chapter 2 ,section 2 Title</title>
            <para>This is a para2</para>
        </sec1>
    </chapter>
    <chapter id="a3">
        <sec1>
            <para>This is a para3, see a reference for <xref href="para1">Here!</xref>
            </para>
        </sec1>
    </chapter>
    <chapter id="a4">
        <sec1 id="sec1chap4">
            <title>Chapter 4 ,section 1 Title</title>
            <para id="para4">This is a para4 <bold>BB</bold>
            </para>
        </sec1>
    </chapter>
    <chapter id="a5">
        <sec1>
            <para>This is a para5 see a reference for <xref href="para4">Here!</xref>
            </para>
        </sec1>
    </chapter>
</root>
Here is a sample xslt:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="para-key" match="para" use="@id"/>
    <xsl:key name="chapter-key" match="chapter" use="descendant::*/@id"/>
    <xsl:key name="section-key" match="sec1" use="descendant::*/@id"/>
    <xsl:template match="/">
        <root>
            <xsl:for-each select="/root/chapter">
                <new>
                    <xsl:for-each select="sec1">
                        <section>
                            <xsl:apply-templates/>
                        </section>
                    </xsl:for-each>
                </new>
            </xsl:for-each>
        </root>
    </xsl:template>
    <xsl:template match="para">
        <para>
            <xsl:apply-templates/>
        </para>
    </xsl:template>
    <xsl:template match="title">
        <title>
            <xsl:apply-templates/>
        </title>
    </xsl:template>
    <xsl:template match="xref">
        <xsl:text>Found para </xsl:text>
        <xsl:value-of select="key('para-key', @href)"/>
        <xsl:text> In chapter </xsl:text>
        <xsl:number level="any" count="chapter" select="key('chapter-key', @href)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="key('section-key', @href)/ancestor::sec1/title"/>
    </xsl:template>
</xsl:stylesheet>
 
Old April 24th, 2009, 11:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Your key call returns a 'sec1' element so doing
key('section-key', @href)/title
suffices to output the title of the referenced sec1 elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 24th, 2009, 11:17 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Ahhh I was over thinking it. thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
<xsl:key elayaraja.s XSLT 1 July 23rd, 2008 05:00 AM
using 3 XPaths to index using xsl:key Chamkaur XSLT 3 March 2nd, 2007 04:09 AM
Using key() and <xsl:key> freddy XSLT 2 January 18th, 2007 08:55 PM
xsl:key doesnt work cristtiah XSLT 11 May 24th, 2006 09:57 AM
selection within xsl:key Kabe XSLT 4 February 25th, 2005 05:49 AM





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