Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - XSLT: using variables

 
Thread Tools Search this Thread
Old 05-14-2006, 02:47 AM   #1
Default XSLT: using variables


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
  Reply With Quote
Old 05-14-2006, 04:57 AM   #2
Joe Kesselman
 
Posts: n/a
Default Re: XSLT: using variables

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


Entirely reasonable; should work...


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  Reply With Quote
Old 05-14-2006, 11:50 AM   #3
Richard Tobin
 
Posts: n/a
Default Re: XSLT: using variables

In article <>,
Joe Kesselman <keshlam-> wrote:

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


>Entirely reasonable; should work...


So long as what you want is section_headers equal to '$text' rather
than the value of the variable $text...

-- Richard
  Reply With Quote
Old 05-14-2006, 02:30 PM   #4
Jeff Higgins
 
Posts: n/a
Default Re: XSLT: using variables


Joe Kesselman wrote:

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


  Reply With Quote
Old 05-14-2006, 02:47 PM   #5
Martin Honnen
 
Posts: n/a
Default Re: XSLT: using variables



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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
  Reply With Quote
Old 05-14-2006, 02:51 PM   #6
Joe Kesselman
 
Posts: n/a
Default Re: XSLT: using variables

Martin Honnen wrote:
> 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/>



--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  Reply With Quote
Old 05-14-2006, 03:45 PM   #7
Jeff Higgins
 
Posts: n/a
Default Re: XSLT: using variables


Martin Honnen wrote:
>
>
> 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>


  Reply With Quote
Old 05-14-2006, 04:02 PM   #8
Jeff Higgins
 
Posts: n/a
Default Re: XSLT: using variables

Correction:

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


  Reply With Quote
Old 05-14-2006, 06:13 PM   #9
Jeff Higgins
 
Posts: n/a
Default Re: using variables

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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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>


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump