Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xsl variable to capture output of call-template returning blank for me

I have seen lots of posts that do something like this and that makes me feel like this is possible and I am just doing something wrong. I have simplified it as much as possible to try and figure out why this is happening:

Heres my xml (nothing very exciting):

<?xml version="1.0" encoding="UTF-8"?>
<REPORT>

</REPORT>

Here is my 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>

Here is my result:

<?xml version="1.0" encoding="utf-8"?>  
<TEST1>
  <MY_NODE desc="my description"/>
</TEST1>
<TEST2></TEST2>
<TEST3></TEST3>

Here is my question: How come TEST2 and TEST3 don't work. The $tryThisTemplate variable appears to be blank. Am I misunderstanding something here. Should I be doing this in a different way?

like image 399
testing123 Avatar asked Jun 13 '11 21:06

testing123


People also ask

How does xsl template match work?

The <xsl: template match=”/”> is an unnamed template and make use of apply-templates to specify the selected node of the input document. It is made mandatory unless it has a name attribute. In layman terms, we can say that an attribute is a pattern that defines which nodes to have and what attributes to apply to them.

What does xsl template element do?

The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

What is text () in XSLT?

The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

Which syntax is used to loop through an xsl document?

XSLT <xsl:for-each> Element The <xsl:for-each> element allows you to do looping in XSLT.


1 Answers

Here is the correct way to do this (note that DOE is not necessary and should be avoided):

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

when this transformation is applied on the provided XML document:

<REPORT>

</REPORT>

the wanted result is produced:

<TEST1>
   <MY_NODE desc="my description"/>
</TEST1>
<TEST2>
   <MY_NODE desc="my description"/>
</TEST2>
<TEST3>
   <MY_NODE desc="my description"/>
</TEST3>

Explanation: <xsl:copy-of> copies (as its name says) nodes. <xsl:value-of> outputs the string value of whatever is in its select attribute. The string value of an element is the concatenation (in document order) of all of its text-node descendents. In your case the element has no text-node descendents and thus <xsl:value-of> outputs nothing.

like image 146
Dimitre Novatchev Avatar answered Oct 21 '22 20:10

Dimitre Novatchev