使用xsl变量为我捕获调用模板返回空白的输出

|| 我看到很多帖子都做了这样的事情,这让我觉得这是可能的,而我只是做错了什么。我已尽可能简化它,以试图弄清楚为什么会这样: 这是我的xml(没什么令人兴奋的):
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<REPORT>

</REPORT>
这是我的xsl:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"2.0\">
    <xsl:template match=\"REPORT\">
      <xsl:variable name=\"tryThisTemplate\">
        <xsl:call-template name=\"TRY_THIS\"/>
      </xsl:variable>
      <TEST1>
        <xsl:call-template name=\"TRY_THIS\"/>
      </TEST1>
      <TEST2>
        <xsl:value-of disable-output-escaping=\"yes\" select=\"$tryThisTemplate\" />
      </TEST2>
      <TEST3>
        <xsl:value-of select=\"$tryThisTemplate\" />
      </TEST3>
    </xsl:template>

    <xsl:template name=\"TRY_THIS\">
      <MY_NODE desc=\"my description\" />
    </xsl:template>
</xsl:stylesheet>
这是我的结果:
<?xml version=\"1.0\" encoding=\"utf-8\"?>  
<TEST1>
  <MY_NODE desc=\"my description\"/>
</TEST1>
<TEST2></TEST2>
<TEST3></TEST3>
这是我的问题: TEST2和TEST3为何不起作用。 $ tryThisTemplate变量似乎为空。我在这里误会了什么吗?我应该以其他方式这样做吗?     
已邀请:
        这是执行此操作的正确方法(请注意,DOE是不必要的,应避免使用):
<xsl:stylesheet
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"2.0\">
<xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>

 <xsl:template match=\"REPORT\">
    <xsl:variable name=\"tryThisTemplate\">
        <xsl:call-template name=\"TRY_THIS\"/>
    </xsl:variable>
    <TEST1>
        <xsl:call-template name=\"TRY_THIS\"/>
    </TEST1>
    <TEST2>
        <xsl:copy-of select=\"$tryThisTemplate\" />
    </TEST2>
    <TEST3>
        <xsl:copy-of select=\"$tryThisTemplate\" />
    </TEST3>
 </xsl:template>

 <xsl:template name=\"TRY_THIS\">
    <MY_NODE desc=\"my description\" />
 </xsl:template>
</xsl:stylesheet>
当此转换应用于提供的XML文档时:
<REPORT>

</REPORT>
所需的结果产生:
<TEST1>
   <MY_NODE desc=\"my description\"/>
</TEST1>
<TEST2>
   <MY_NODE desc=\"my description\"/>
</TEST2>
<TEST3>
   <MY_NODE desc=\"my description\"/>
</TEST3>
说明:“ 6”复制(如其名称所示)节点。
<xsl:value-of>
输出其
select
属性中任何值的字符串值。元素的字符串值是其所有文本节点后代的串联(按文档顺序)。在您的情况下,该元素没有文本节点后代,因此
<xsl:value-of>
不输出任何内容。     
        是的,这里有一个误会。如果尝试将
$tryThisTemplate
的结构复制到输出,则需要使用
<xsl:copy-of>
而不是
<xsl:value-of>
。 “ 7”输出其选择参数的字符串值,即其文本内容,在这种情况下为空字符串。     
           $ tryThisTemplate变量似乎为空 变量不为空,但使用
xsl:value-of
,您需要在其中的文本节点。那是“空白”。 例如,尝试:
  <TEST3>
    <xsl:copy-of select=\"$tryThisTemplate\" />
  </TEST3>
然后您会看到在
TEST3
之间神奇地出现了
MY_NODE
:))     
        看看我的解决方案 这是我的模板(内容真实)
<xsl:template name=\"author-name\" match=\"string-name\">
    <xsl:if test=\"fn[string-length(normalize-space()) > 0] or given-names[string-length(normalize-space()) > 0]\">
        <xsl:apply-templates select=\"fn | given-names\" mode=\"ascii\"/>
    </xsl:if>
    <xsl:if test=\"sn[string-length(normalize-space()) > 0] or surname[string-length(normalize-space()) > 0]\">
        <xsl:text> </xsl:text><xsl:apply-templates select=\"sn | surname\" mode=\"ascii\"/>
    </xsl:if>
    <xsl:if test=\"sn[string-length(normalize-space()) > 0] or suffix[string-length(normalize-space()) > 0]\">
        <xsl:text> </xsl:text><xsl:apply-templates select=\"sn | suffix\" mode=\"ascii\"/>
    </xsl:if>
</xsl:template>
当我在其他任何模板中使用它时,我只是这样称呼它
<xsl:template match=\"string-name\">
    <xsl:variable name=\"author\">
        <xsl:call-template name=\"author-name\"/> <!--here is the tricky part-->
    </xsl:variable>
    <span class=\"NLM_{name()}\">
        <xsl:copy-of select=\"@id\" />
        <xsl:value-of select=\"normalize-space($author)\" />
    </span>   
</xsl:template>
希望这对您有帮助     

要回复问题请先登录注册