I'm transforming xsl document to html like this:
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
Blah-blah
</body>
</html>
</xsl:template>
Is it right way? Or, maybe, using of xsl:element
is better? I didn't saw examples with such variant:
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="head">
<xsl:element name="title">
Title
</xsl:element>
</xsl:element>
<xsl:element name="body">
Blah-blah
</xsl:element>
</xsl:element>
</xsl:template>
Which variant is right?
Best regards.
A literal result element (i.e. your first approach) is shorter, easier to type and easier to read. I would suggest to use xsl:element
only in cases where you want to compute the element name and/or namespace dynamically based on input data e.g.
<xsl:template match="*">
<xsl:element name="{translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
In other cases I would use literal result elements as in your first sample. But there is no right or wrong in terms of the result, both variants give the same result tree.
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