The first installment of the two-part series on XSLT introduces some of the new features of XSLT 2.0 and explains how to generate code from an abstract data model. To demonstrate this process, I built a project, developed a robust code generator, generated SQL for the database server, and generated PHP for the WEB server to access the database. I use multi-tier transformations to build SQL from XSL. The abstract model is first transformed into a database physical schema model, and then the schema model is used to build the SQL code.
The next step is to build the code model and generate PHP from the model. When you end a project, you will have an abstract model of the system, SQL code to build the database, and a PHP wrapper for each table. But before delving into the PHP build, I want to first review the new features that affect the XSL and XSLT 2.0 that are designed and used by XSL templates.
Enhancements to an XSL template
To generate successful code, you need to have a good understanding of the target language (this is PHP) and the Code generation language (XSLT in this case). The 1th part focuses on the fundamentals of code generation. This article provides a more in-depth analysis of the XSL end of the code generation equation.
In essence, XSLT is a templated language. It takes XML as input and then uses a set of templates to translate XML into XML, HTML, or text. The builder is a collection of related templates, using two modes--xml mode and Text mode-converts the original input XML into code. The intermediate model between the abstract model and the code template uses the XML schema, and the code templates that generate PHP and SQL use text patterns.
This code generation system uses the Saxon XSLT engine and a set of custom templates. For convenience, these templates and inputs are placed in the same directory. The template output is placed in the PHP and SQL code directories, respectively. No special extensions are required for Saxon, although if you find that the basic installation provides an XSL tag or XPath function that is not sufficient, you can use Java? Extend the template engine.
The entry for the XSL template is the XSL template that matches the root node of the input XML. When the XSL engine starts, it applies the input XML to the template library. If there is a special template that matches the root node (/), it is executed first. The following is the main template label in the generator:
<xsl:template match="/">
This matching system is important because XSL wants to see a list of available templates that are currently working on the node, and then apply the most matching template. Look at the code in Listing 1, which is an example of an article in the previous installment.
Listing 1. Template with pattern type
<xsl:template match="create" mode="sql">
DROP TABLE IF EXISTS <xsl:value-of select="@name" />;
CREATE <xsl:value-of select="@name" /> (
<xsl:apply-templates mode="sql" select="field" />
PRIMARY KEY ( <xsl:value-of select="@primary-key" /> )
);
</xsl:template>
The code tells the XSL that the template is applied to the create tag. Therefore, when the XSL encounters the Create label, it executes the template. Also, be aware that the template specifies a pattern. You need to specify the pattern in the xsl:apply-templates tag as in Listing 2.
Listing 2. Apply a label for a template
<xsl:apply-templates mode="sql" select="$sql-model/sql" />
Patterns in the XSL
Patterns are an important concept of XSL. Because there may be multiple XML hierarchies in memory, there is a way to apply a special set of templates to a particular hierarchy, or a set of transformations that can be used for a single XML source. So the model is introduced. This code generator has a set of templates in PHP as a pattern, and the other group in SQL. Distinguish the template logic of the two languages by using patterns.
The xsl:apply-templates tag tells the XSL to apply the template that can be used to the part specified in the select tag in the XML model. In addition, schema SQL is defined, so the XSL only looks for templates that use SQL mode. This is a convenient template namespace mechanism.