Subject: Re: [xsl] referencing elements From: Peter Davis <pdavis152@xxxxxxxxx> Date: Thu, 28 Mar 2002 19:18:05 -0800 |
You want to use the current() function in order to do "chapter[@name=current()/id]". You are right that the brackets "override" the current context node; the current() function gets the context node in the containing <xsl:for-each> or <xsl:template>. But your templates are a little confused over what the context is (not to mention syntax). See below. On Thursday 28 March 2002 17:53, Andreas Leitner wrote: > I have a xml input document like this: > <root> > <chapter name="foo"> > this is all about foo > </chapter> > <chapter name="bar"> > this is all about bar > </chapter> > <chapter name="baz"> > this is all about baz. > you might want to have a look at chapter > <link id="bar"/> too. > </chapter> > </root> > > Now I would like to generate some nice html out of this and convert the > "link" element into a true hyperreference > > therefore in my stylesheet i have something along the lines (not with > true hyperlinks yet, but just to demonstrate ids: > <xsl:template select="chapter" (you want <template *match*="chapter">, and missing a '>' here, but you'd probably figure that out) > id of this chapter is: <xsl:value-of select"generate-id()"/> > <br/> > to demonstrate a link: > <xsl:value-of select="/chapter[@name=???]"/> Here, you want "/root/chapter[...]". There is no such thing as "/chapter" in your source document. The other problem here is that I assume you want to reference the chapter that has the same name as the <link>'s @id. But your template is matching "chapter", not "link". Therefore, assuming that it is possible to have more than one link in the same chapter, the template doesn't have direct access to the <link>. If you just want to output a list of all the links at the top of the chapter, you could do something like this: <xsl:template match="chapter"> ... <xsl:for-each select="link"> <br/> To demonstrate a link: <xsl:value-of select="/root/chapter[@name = current()/@id]"/> </xsl:for-each> </xsl:template> If you are outputting HTML, then something like this: <xsl:for-each select="link"> <a href="#{generate-id(/root/chapter[@name = current()/@id])}"> <xsl:value-of select="."/> </a> </xsl:for-each> If you want to have the links embedded within the chapter, then you will have to move that code from a <xsl:for-each> to a <xsl:template match="link">, and then <xsl:apply-templates/> somewhere in the <xsl:template match="chapter">. Hope that makes sense. You might also want to look up key() and <xsl:key/>, since they provide an alternative and probably better way of doing this. > </xsl:template> > > Now the ??? part is what i don't know how to solve. My first go was to > write: > select="/chapter[@name=@id]" > or: > select="/chapter[@name=./@id]" > > Hoping that somehow i could access the node that the current template > rule matched, but it seems in square brackets the context node is > overruled. > > Anybody knows how to do this? > > Also, I would like to produce one html page per chapter. Is there a > chance I can do this without post-processing the result? > > thank you very much in advance, > Andreas > > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list -- Peter Davis Television -- the longest amateur night in history. -- Robert Carson XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] referencing elements, Andreas Leitner | Thread | Re: [xsl] referencing elements, Andreas Leitner |
[xsl] Re: Getting the attributes & , Shaik Zulfakhar Ali | Date | [xsl] figuring out if first or last, Eric K Taylor |
Month |