Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xsl:element tag for html transformation

Tags:

html

tags

xslt

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.

like image 267
bsiamionau Avatar asked Oct 06 '22 03:10

bsiamionau


1 Answers

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.

like image 106
Martin Honnen Avatar answered Oct 13 '22 11:10

Martin Honnen