Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: using variables

Reply
Thread Tools

XSLT: using variables

 
 
Jeff Higgins
Guest
Posts: n/a
 
      05-14-2006
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


 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      05-14-2006
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
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-14-2006
In article <(E-Mail Removed)>,
Joe Kesselman <(E-Mail Removed)> 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
 
Jeff Higgins
Guest
Posts: n/a
 
      05-14-2006

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
 
Martin Honnen
Guest
Posts: n/a
 
      05-14-2006


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
 
Joe Kesselman
Guest
Posts: n/a
 
      05-14-2006
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
 
Jeff Higgins
Guest
Posts: n/a
 
      05-14-2006

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
 
Jeff Higgins
Guest
Posts: n/a
 
      05-14-2006
Correction:

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


 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      05-14-2006
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

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Using variables as variables Frank Javascript 4 04-10-2007 04:20 PM
Help with Javascript: naming variables using other variables sagejoshua HTML 16 11-09-2005 12:27 PM
Using explicitly sized variables in functions as auto variables or parameters Adel C Programming 3 03-17-2005 04:36 AM
Using match variables ($1, $2 ...) as variables. Ravi Parimi Perl Misc 16 05-13-2004 07:03 PM



Advertisments