anderci
anderci

Reputation: 41

XSLT: Why cannot I separate Use: xsl:value-of from Define: xsl:variable?

I do not know how to 'declare' the Author variable separately at top.

If I move the value-of... segment outside of the choose... block, it does not work. This is the desired result: "Author" "Ansay, A_Manette " "Elkins, Aaron " "Ross, Adam " "Craig, Alisa "

This template does work:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
    <xsl:variable name="nl"> 
      <xsl:text>&#10;</xsl:text> 
    </xsl:variable>
    <xsl:variable name="d-quote"> 
      <xsl:text>&quot;</xsl:text>
    </xsl:variable>  
    <!-- <xsl:variable name="Author"/>  -->

  <xsl:template match="mysteries">
    <xsl:value-of select="concat($d-quote,'Author',$d-quote,$nl)"/>    
    <xsl:apply-templates select="author"/>  
  </xsl:template>

  <xsl:template match="author">
    <xsl:choose>  
      <xsl:when test="
        substring-before(
          substring-before(
            substring-after(
              substring-after(node(),' - '),
              ' '),
            $nl),
          ' ') 
        = ''">  
        <xsl:variable name="Author">
          <xsl:value-of select="concat(
            $d-quote,
            substring-before(
              substring-before(
                substring-after(
                  substring-after(node(),' - '),
                  ' '),
                ' '),
              $nl), 
            ', ', 
            substring-before(
              substring-after(node(),' - '),
              ' '), 
           ' ', 
           $d-quote)"/> 
        </xsl:variable>  
        <xsl:value-of select="concat($Author,$nl)"/>   
      </xsl:when>  
      <xsl:otherwise>  
        <xsl:variable name="Author">
          <xsl:value-of select="concat(
            $d-quote,
            substring-before(
              substring-before(
                substring-after(
                  substring-after(node(),' - '),
                  ' '),
                $nl),
              ' '), 
            ', ', 
            substring-before(
              substring-after(node(),' - '),
              ' '), 
            ' ', 
            $d-quote)"/> 
        </xsl:variable>  
        <xsl:value-of select="concat($Author,$nl)"/>   
      </xsl:otherwise>
    </xsl:choose>  
    <!-- <xsl:value-of select="concat($Author,$nl)"/>   -->
   </xsl:template>  

 </xsl:stylesheet>

For this XML data:

<mysteries>
<author>Mystery - A_Manette Ansay
  <title>A_Manette Ansay</title>
  <title>   http://www.amanetteansay.com/wordpress/</title>
  <title>mystery=</title>
  <title>'Vinegar Hill'</title>
  <title>'Read This and Tell Me What It Says'</title>
  <title>'Sister'  1996  </title>
  <title>'River Angel'</title>
  <title>'Midnight Champagne'</title>
  <title>'Limbo'</title>
  <title>'Blue Water'</title>
  <title>'Good Things I Wish You'</title>
</author>
<author>Mystery - Aaron Elkins -
  <title>Aaron Elkins</title>
  <title>   http://www.booksnbytes.com/authors/taylor_alisong.html</title>
  <title>       http://www.aaronelkins.com/</title>
  <title>mystery=</title>
  <title>Gideon Oliver - Forensic Anthropologist / Professor</title>
  <title></title>
  <title>'Fellowship of Fear'  1982 </title>
  <title>'The Dark Place'  1983</title>
  <title>'Murder in the Queen's Armes'  1985</title>
  <title>x'Old Bones'  1987  </title>
  <title>'Curses'  1989</title>
  <title>'Icy Clutches'  1990</title>
  <title>'Make No Bones'  1991</title>
  <title>'Dead Men's Hearts'  1994</title>
  <title>'Twenty Blue Devils'  1997</title>
  <title>'Skeleton Dance'  2000</title>
  <title>'Good Blood'  2004</title>
  <title>x'Where There's a Will'  2005 </title>
  <title>'Unnatural Selection'  2006</title>
  <title>'Little Tiny Teeth'  2007</title>
  <title>'Uneasy Relations'  2008</title>
  <title></title>
  <title></title>
  <title></title>
  <title>Chris Norgren - Curator - Seattle Art Museum</title>
  <title>'Old Scores'</title>
  <title>'A Glancing Light'</title>
  <title>'A Deceptive Clarity'</title>
</author>
<author>Mystery - Adam Ross
  <title>Adam Ross </title>
  <title>   http://adam-ross.com/</title>
  <title>novels= </title>
  <title>#'Mr. Peanut'  2010  </title>
  <title>'Ladies and Gentlemen'  </title>
</author>
<author>Mystery - Alisa Craig (Charlotte MacLeod) - Grub-and-Stakers Senior Sleuths
  <title>Alisa Craig    (Charlotte MacLeod)</title>
  <title>   http://www.booksnbytes.com/authors/craig_alisa.html</title>
  <title>http://www.fictiondb.com/author/charlotte-macleod~book-list-pseudonyms~21573.htm</title>
  <title> mystery> Grub-and-Stakers Senior Sleuths</title>
  <title>x'The G and S Move a Mountain' 1981  OWN</title>
  <title>x'The G and S Quilt a Bee' 1985     </title>
  <title>'The G and S Pinch a Poke' 1988   </title>
  <title>x'The G and S Spin a Yarn' 1990   </title>
  <title>x'The G and S House a Haunt' 1993   </title>
  <title></title>
  <title></title>
  <title>x'The Terrible Tide'  1983  </title>
</author>
</mysteries>

Upvotes: 0

Views: 204

Answers (1)

nwellnhof
nwellnhof

Reputation: 33658

The XSLT 1.0 spec says:

As well as being allowed at the top-level, both xsl:variable and xsl:param are also allowed in templates. xsl:variable is allowed anywhere within a template that an instruction is allowed. In this case, the binding is visible for all following siblings and their descendants.

So variables in templates have block scope. They're not visible outside of the containing element. A work-around for your example would be to move the xsl:choose block inside the xsl:variable:

<xsl:variable name="Author">
    <xsl:choose>
        <xsl:when test="...">
            <xsl:value-of select="..."/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="..."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($Author,$nl)"/>

Upvotes: 2

Related Questions