Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use XSL to create HTML attributes?

Tags:

Why is using XML data to style HTML tags illegal? For example:

<li style="width:<xsl:value-of select="width"/>px">  

Why can't I do this? Are there any alternative methods out there?

like image 879
stockoverflow Avatar asked Feb 23 '11 11:02

stockoverflow


People also ask

Can I use xsl in HTML?

XSL can be used server-side and client-side. The XSL Submission has two classes of output: DSSSL-style flow objects and HTML tags.

How create xsl file in HTML?

Creating the basic HTML document<xsl:template match="/"> <html> <head> <title> <xsl:value-of select="/myNS:Article/myNS:Title"/> </title> <style> .

Can XML generate HTML?

You can generate HTML documentation from an XML Schema file. The HTML documentation contains various information about the message model that is included in the XML Schema file.

How do I convert XML to HTML?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.


2 Answers

Why can't I do this?

<li style="width:<xsl:value-of select="width"/>px"> 

Because XSL is XML itself. And this is anything… but not XML.

You mean an Attribute Value Template:

<li style="width:{width}px"> 

or the explicit form, for more complex expressions:

<li>   <xsl:attribute name="style">     <xsl:choose>       <xsl:when test="some[condition = 'is met']">thisValue</xsl:when>       <xsl:otherwise>thatValue</xsl:otherwise>     </xsl:choose>   </xsl:attribute> </li> 

or dynamic attribute names (note that attribute value template in the name):

<li>   <xsl:attribute name="{$attrName}">someValue</xsl:attribute> </li> 

Additional note: Attributes must be created before all other child nodes. In other words, keep <xsl:attribute> at the top.

like image 121
Tomalak Avatar answered Oct 06 '22 10:10

Tomalak


Your original xsl is not well formed as you can't have the xsl tag inside another node.

I think you need to use xsl:attribute as follows:

<li>   <xsl:attribute name="style">      width:<xsl:value-of select="width"/>px;   </xsl:attribute> </li> 
like image 36
Jon Egerton Avatar answered Oct 06 '22 08:10

Jon Egerton