XSLT:为什么我不能单独使用:xsl:value-of来自Define:xsl:variable?

时间:2013-11-30 19:00:54

标签: xml xslt-1.0

我不知道如何在顶部单独“声明”作者变量。

如果我将...段的值移到choose ...块之外,则它不起作用。 这是期望的结果: “作者” “Ansay,A_Manette” “埃尔金斯,亚伦” “罗斯,亚当” “Craig,Alisa”

此模板确实有效:

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

对于此XML数据:           

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

1 个答案:

答案 0 :(得分:2)

XSLT 1.0 spec说:

  

除了被允许进入顶层之外,模板中也允许xsl:variablexsl:param。允许指令的模板中允许xsl:variable。在这种情况下,所有后续兄弟姐妹及其后代都可以看到绑定。

因此模板中的变量具有块范围。它们在包含元素之外是不可见的。您的示例的解决方法是在xsl:choose内移动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)"/>