XSLT: using variables

Discussion in 'XML' started by Jeff Higgins, May 14, 2006.

  1. Jeff Higgins

    Jeff Higgins Guest

    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, May 14, 2006
    #1
    1. Advertising

  2. 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
    Joe Kesselman, May 14, 2006
    #2
    1. Advertising

  3. In article <>,
    Joe Kesselman <> 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
    Richard Tobin, May 14, 2006
    #3
  4. Jeff Higgins

    Jeff Higgins Guest

    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>
    Jeff Higgins, May 14, 2006
    #4
  5. 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/
    Martin Honnen, May 14, 2006
    #5
  6. 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
    Joe Kesselman, May 14, 2006
    #6
  7. Jeff Higgins

    Jeff Higgins Guest

    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>
    Jeff Higgins, May 14, 2006
    #7
  8. Jeff Higgins

    Jeff Higgins Guest

    Correction:

    <xsl:variable name="anchortext" select="$anchor/section/anchor |
    $anchor/chapter/anchor"></xsl:variable>
    Jeff Higgins, May 14, 2006
    #8
  9. Jeff Higgins

    Jeff Higgins Guest

    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 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>
    Jeff Higgins, May 14, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    636
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,658
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    707
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    9
    Views:
    927
Loading...

Share This Page