X

The Integration blog covers the latest in product updates, best practices, customer stories, and more.

  • December 11, 2015

12c XSLT Editor – XSLT View - Refactoring

Guest Author

XSLT View is one of two graphical views provided by the new
12c XSLT Editor.  This blog post will
discuss refactoring support in the XSLT editor XSLT View for creating a
reusable matched template.

(For a quick introduction to the graphical views in the 12c XSLT
Editor see: https://blogs.oracle.com/integration/entry/12c_xslt_editor_overview.)

For this post I’d like to continue with the example we were
working with in Map View.  (See post https://blogs.oracle.com/integration/entry/12c_xslt_editor_map_view
). In this example we had created a map
from a purchase order to an invoice using Map View of the XSLT Editor.

We have mapped the Address information under the source
ShipTo node to the target ShippedTo address. 
We also have BillTo address information that needs to be mapped. The underlying address structures for the
BillTo and ShipTo addresses are the same, so it would make sense to create a
single template that contains the generic mapping of address information and
reuse that template for both the BillTo and ShipTo address mappings.

We will do this by refactoring the existing mappings from
ShipTo to ShippedTo into a separate matched template. Then we can invoke that template using the
xsl:apply-templates statement where needed to populate the ShippedTo and
BIlledTo addresses in the output xml.

We start by moving to XSLT view. Clicking the XSLT button on the right of the
editor toolbar takes us to XSLT view.

XSLT View shows us the split view of the XSLT (top-right)
and target schema tree (bottom-right). 

We need to duplicate/refactor the mappings under the
ShippedTo node in the XSLT into a separate template. Opening the ShippedTo area in the XSLT panel
shows us the following:

 Right-click the ShippedTo/Address node in the XSLT panel and
select Create in Template.

The New Template Rule dialog is displayed with a suggested
match string for the new template. 
Selecting OK on this dialog will trigger the refactoring to occur.

 A new matched template is created containing the original
mappings for the Address node.  An
xsl:apply-templates statement is inserted at the original position of the Address
node in the XSLT. This
xsl:apply-templates statement will cause the new matched template to execute at
this point in the XSLT causing the Address output node to be created at this
position in the xml output.

The ShipTo/Address node in the source tree is highlighted
with a purple bubble meaning that it is the context node for the new template
(relative xpath expressions in the new template are relative to this context
node).

Now we would like to reuse this matched template to map our
BillTo address information. First we
will make the match string in the matched template generic enough to use for
both areas. Double-clicking the new
xsl:template node, we get a dialog where we can change the attributes of the
xsl:template statement.

 The attribute information can also be edited in the
Properties Window.  After clicking OK,
the match string in the template is changed from /ns0:PurchaseOrder/ShipTo/Address
to Address. This will match all nodes
named Address.

 While the match string is now Address and is capable of
matching any node in the source named Address, the execution of the template is
limited by the select expression defined for the xsl:apply-templates.   The match string is Address and the select
in the xsl:apply-templates is /ns0:PurchaseOrder/ShipTo/Address. In order to execute the Address matched
template for the BilledTo area, we need to add another xsl:apply-templates
statement under the output BilledTo node.

To add the BilledTo node to the XSLT, right-click its parent
node (tns:Invoice) and select Add Children From Schema > BilledTo.

 The BilledTo node is added along with all required children.

 We will place an xsl:apply-templates element in place of the
Address node in the same manner as for the ShippedTo node.   We delete the Address node and select an
xsl:apply-templates element to append as a child of the BilledTo element:

 Map the BillTo/Address field to the xsl:apply-templates
statement to invoke the template.

 Then map the Name field in the same manner as under ShippedTo.   

 When we click back on the Address template, we now have two
context nodes for the template.

Note the relative xpath expressions used within the Address
template.  The City xpath expression is
selected. City is relative to the two
Address context nodes.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha