XSLT: using variables

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
Hi,

In the variable "anchor" I would like to,
(I think), use the value of the variable "text"
in the predicate of the select expression.
Is this possible?

Is there a better way of doing
what I think I want to do?

Is there an easier way of using the values
of variables other than thru the value-of element?

<xsl:template match="addinfo">

<xsl:variable name="text"
select="addinfo_link_text"/>

<xsl:variable name="prefix"
select="addinfo_prefix"/>

<xsl:variable name="anchor"
select="//section/anchor
[../section_header = '$text']"/>

<xsl:variable name="page"
select="addinfo_pg_no"></xsl:variable>

<xsl:variable name="postfix"
select="addinfo_postfix"/>

<span><a>
<xsl:attribute name="href"><xsl:value-of
select="$anchor"/></xsl:attribute>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$text"/>
<xsl:value-of select="$postfix"/>
<xsl:value-of select="$page"/>
</a></span>

<xsl:apply-templates/>
</xsl:template>

Thanks
Jeff Higgins
 

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
Joe said:
Entirely reasonable; should work...

really can't seem to make the right selection
for the anchor variable?

Other problems too. But I'll work on that.

Sorry for the bulky post.

Result:

<span><a href="">For further information, See "User Interface" on page
10.10</a></span>
<span><a href="">For further information, See "Menus" on page
20.20</a></span>
<span><a href="">For further information, See "Getting Started" on page
1.1</a></span>

Desired output:

<span><a href="2_UserInterface">For further information, See "User Interface" on page 10.</a></span> <span><a href="3_Menus">For further information, See "Menus" on page
20.</a></span>
<span><a href="1_GettingStarted">For further information, See "Getting Started" on page 1.</a></span>

<xsl:template match="info">
<xsl:variable name="text" select="text"/>
<xsl:variable name="anchor" select="ancestor::anchor[../header = '$text']"/> <span> <a> <xsl:attribute name="href"><xsl:value-of select="$anchor"/></xsl:attribute> <xsl:value-of select="prefix"/> <xsl:value-of select="text"/> <xsl:value-of select="postfix"/> <xsl:value-of select="postfix/page"/> </a> </span> <xsl:apply-templates/> </xsl:template>

<chapter>
<anchor>1_GettingStarted</anchor>
<header>Getting Started</header>
<info>
<prefix>For further information, See "</prefix>
<text>User Interface</text>
<postfix>" on page <page>10</page>.</postfix>
</info>
<section>
<anchor>2_UserInterface</anchor>
<header>User Interface</header>
<info>
<prefix>For further information, See "</prefix>
<text>Menus</text>
<postfix>" on page <page>20</page>.</postfix>
</info>
</section>
<section>
<anchor>3_Menus</anchor>
<header>Menus</header>
<info>
<prefix>For further information, See "</prefix>
<text>Menus</text>
<postfix>" on page <page>100</page>.</postfix>
</info>
</section>
</chapter>
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Jeff Higgins wrote:

<xsl:variable name="text" select="text"/>
<xsl:variable name="anchor" select="ancestor::anchor[../header = '$text']"/>

As Richard has already pointed out, you most likely want/need

<xsl:variable name="anchor" select="ancestor::anchor[../header = $text]"/>

as otherwise (with '$text') you simply compare to the literal string with '$text' and not to a variable value.
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
Martin said:
as otherwise (with '$text') you simply compare to the literal string with '$text' and not to a variable value.

Whups. I shoulda caught that. That's what I get for not testing before posting. <grin/>
 

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
Martin said:
Jeff Higgins wrote:

<xsl:variable name="text" select="text"/>
<xsl:variable name="anchor" select="ancestor::anchor[../header = '$text']"/>

As Richard has already pointed out, you most likely want/need

<xsl:variable name="anchor" select="ancestor::anchor[../header = $text]"/>

as otherwise (with '$text') you simply compare to the literal string with '$text' and not to a variable value.

Yes. Thanks, I did catch that, although you couldn't tell it from my most recent (faulty) post.

Sorry for changing examples in mid-thread.

As it turns out, the following template does very nearly what I want:

<xsl:template match="addinfo">
<xsl:variable name="text" select="addinfo_link_text"/>
<xsl:variable name="anchor" select="ancestor::section[./section_header = $text] | ancestor::chapter[./chapter_header = $text]"/> <xsl:variable name="anchortext" select="$anchor/section/section_header | $anchor/chapter/chapter_header"></xsl:variable> <span> <a> <xsl:attribute name="href"><xsl:value-of select="$anchortext"/></xsl:attribute> <xsl:value-of select="addinfo_prefix"/> <xsl:value-of select="addinfo_link_text"/> <xsl:value-of select="addinfo_postfix"/> <xsl:value-of select="addinfo_postfix/addinfo_pg_no"/> </a> </span> <xsl:apply-templates/> </xsl:template>
 

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
Correction:

<xsl:variable name="anchortext" select="$anchor/section/anchor | $anchor/chapter/anchor"></xsl:variable>
 

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
Hi,

I made kind of a mess of this thread by
being in a hurry, but anyway, for the
sake of posterity the following produces
the desired result.

Thanks everybody.
Jeff Higgins

Desired and achieved result:
<span>For further information on this subject, see "<a
href="1_UserInterface">User Interface</a>" on page 10.</span> <span>For further information on this subject, see "<a href="1_1_1_Menus">Menus</a>" on page 20.</span> <span>For further information on this subject, see "<a href="1_GettingStarted">Getting Started</a>" on page 1.</span>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:eek:utput indent="yes"/> <xsl:template match="/|@*|node"> <xsl:apply-templates/> </xsl:template> <xsl:template match="addinfo"> <xsl:variable name="text" select="addinfo_link_text"/> <xsl:variable name="anchor" select="//section[section_header = $text] | //chapter[chapter_header = $text]"/> <xsl:variable name="anchortext" select="$anchor/anchor | $anchor/anchor"/> <span> <xsl:value-of select="addinfo_prefix"/> <a> <xsl:attribute name="href"><xsl:value-of select="$anchortext"/></xsl:attribute> <xsl:value-of select="addinfo_link_text"/> </a> <xsl:value-of select="addinfo_postfix/text[1]"/> <xsl:value-of select="addinfo_postfix/addinfo_pg_no"/> <xsl:value-of select="addinfo_postfix/text[2]"/> </span> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>

<chapter>
<anchor>1_GettingStarted</anchor>
<chapter_header>Getting Started</chapter_header>
<paragraph>This section covers the fundamental</paragraph> <paragraph> <addinfo> <addinfo_prefix>For further information on this subject, see "</addinfo_prefix> <addinfo_link_text>User Interface</addinfo_link_text> <addinfo_postfix>" on page <addinfo_pg_no>10</addinfo_pg_no>.</addinfo_postfix> </addinfo> </paragraph> <section> <anchor>1_UserInterface</anchor> <section_header>User Interface</section_header> <paragraph>This section provides information on</paragraph> <paragraph> <addinfo> <addinfo_prefix>For further information on this subject, see "</addinfo_prefix> <addinfo_link_text>Menus</addinfo_link_text> <addinfo_postfix>" on page <addinfo_pg_no>20</addinfo_pg_no>.</addinfo_postfix> </addinfo> </paragraph> <section> <anchor>1_1_ComponentsoftheUI</anchor> <section_header>Components of the UI</section_header> <paragraph>The main area of the screen is </paragraph> <paragraph> <addinfo> <addinfo_prefix>For further information on this subject, see "</addinfo_prefix> <addinfo_link_text>Getting Started</addinfo_link_text> <addinfo_postfix>" on page <addinfo_pg_no>1</addinfo_pg_no>.</addinfo_postfix> </addinfo> </paragraph> <section> <anchor>1_1_1_Menus</anchor> <section_header>Menus</section_header> <paragraph>The main menu contains</paragraph> </section> <section> <anchor>1_1_2_Toolbars</anchor> <section_header>Toolbars</section_header> <paragraph>The toolbars are located</paragraph> </section> </section> </section> </chapter>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top