Search code examples
htmlxmlxsltmeta

How to dynamically add meta tag using XSLT


I have a specific problem where I have to add a meta tag to a HTML using XSLT.

What I need to do is to keep the body and add a meta tag with the content of the tag 'option'.

The code that I want is something like that, or something that do what this was supposed to do:

 <xsl:template match="/">
  <html>
    <head>
      <meta name="option" content="   <xsl:value-of select="//option" />   " />
    </head>
    <body>
      <xsl:value-of select="html/body" />
    </body>
  </html>
</xsl:template>

You can see that the content of the meta tag is also a value that I want to get from the document.

For example, if I use the following code, it works fine:

 <xsl:template match="/">
  <html>
    <head>
      <meta name="option" content="Test" />
    </head>
    <body>
      <xsl:value-of select="html/body" />
    </body>
  </html>
</xsl:template>

However, the content will be fixed as "Test".

There is any way to add a dynamic meta tag using just XSLT?

Thank you in advance.


Solution

  • You can use an Attribute Value Template wrapping the XPath expression in { and } like

    <meta name="option" content="{//option}" />