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?
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.
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).
The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
XSLT <xsl:for-each> Element The <xsl:for-each> element allows you to do looping in XSLT.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With