1. Problem
You want to use the inline
XSLT call template functionality within the Scripting functoid and
understand the difference between inline XSLT and an inline XSLT call
template.
2. Solution
Use the following steps to add
an Inline XSLT Call Template functoid call to a map. The steps assume
the schemas shown in the map in Figure 1 are being used.
Click
the toolbox, and then click the Advanced Functoids tab. Drop a
Scripting functoid onto the map surface.
While the Scripting functoid is highlighted on the
mapping grid, click the ellipsis to the right of the FunctoidScript item in the Properties window. In the
Configure Scripting Functoid dialog box, on the Script Functoid
Configuration tab, select Inline XSLT Call Template for Script Type,
place the code shown in Listing 1 into the Inline Script Buffer text box, and then click OK.
Example 1. Template Code
<xsl:template name="CreateCompanySchema"> <xsl:param name="ID" /> <xsl:param name="Name" /> <xsl:param name="Role" /> <xsl:param name="Age" /> <xsl:element name="Company"> <xsl:element name="ID">Default Company ID</xsl:element> <xsl:element name="Name">Default Company Name</xsl:element> <xsl:element name="Employees"> <xsl:element name="Employee"> <xsl:element name="ID"><xsl:value-of select="$ID" /></xsl:element> <xsl:element name="Name"><xsl:value-of select="$Name" /></xsl:element> <xsl:element name="Role"><xsl:value-of select="$Role" /></xsl:element> <xsl:element name="Age"><xsl:value-of select="$Age" /></xsl:element> </xsl:element> </xsl:element> </xsl:element> </xsl:template>
|
Create four
input parameters by dropping each of the four nodes (ID, Name, Role,
and Age) in the source document onto
the Scripting functoid.
Drop the output of
the functoid onto the root node (Company)
of the destination schema.
Test the map.
Assuming that the input document that is shown in Listing 2 is used as input, the document
shown in Listing 3 will be output.
Example 2. Input Document
for the Call Template Example
<ns0:Person xmlns:ns0="http://UsingCallTemplate.Person"> <ID>1</ID> <Name>S. Brekalo</Name> <Role>Acupuncturist</Role> <Age>33</Age> </ns0:Person>
|
Example 3. Output
Document for the Call Template Example
<Company> <ID>Default Company ID</ID> <Name>Default Company Name</Name> <Employees> <Employee> <ID>1</ID> <Name>S. Brekalo</Name> <Role>Acupuncturist</Role> <Age>33</Age>
</Employee> </Employees> </Company>
|
3. How It Works
Calling an XSLT template is
very similar to using inline XSLT. The main difference is the way in
which values within the source document are passed and accessed. With
inline XSLT, node values in the source document are accessed through XSL
methods, whereas with called XSLT templates, the values are passed in
as parameters.
In the case where code may
need to be reused for multiple nodes, it may be more advantageous to
create a template that can be reused without modifying the code.
Templates will also allow for more traditional programming techniques,
such as setting and updating variables dynamically within the template
(for example, the ability to update a variable to store the number of
loops that have occurred within a for-each loop).
Listing 4 demonstrates the use of inline XSLT rather than a
called XSLT template. The output of Listing 4 will produce the same output as that of the XSLT
template code shown earlier in Listing 1.
Example 1. Inline XSLT for Comparison
<xsl:element name="Company">
<xsl:element name="ID">Default Company ID</xsl:element>
<xsl:element name="Name">Default Company Name</xsl:element>
<xsl:element name="Employees">
<xsl:element name="Employee">
<xsl:element name="ID">
<xsl:value-of select="//*[local-name()='ID']" />
</xsl:element>
<xsl:element name="Name">
<xsl:value-of select="//*[local-name()='Name']" />
</xsl:element>
<xsl:element name="Role">
<xsl:value-of select="//*[local-name()='Role']" />
</xsl:element>
<xsl:element name="Age">
<xsl:value-of select="//*[local-name()='Age']" /></xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>