Jump to content

HTML attribute from XML


lknight

Recommended Posts

I want to to some CSS formating based on individual xml item id's. Therefore, I need XSLT to convert the xml id attribute to an html if attribute. XML:

<data><item id="0"><content>bla</content></item><item id="1"><content>bla</content></item></data>

XSLT:

<div id="<xsl:value-of select='@id'>"> <!-- Problem line -->	<xsl:apply-templates select="@id"/> 	<xsl:apply-templates select="content"/></div>

Of course a value-of within a <div> is invalid. I tried using > and <, but to no effect. Is there any way (apart from external scripts) to put the value of my xml id attribute into the html id attribute?I'm also open to another approach if anyone has one...

Link to comment
Share on other sites

Oh, how simple. XSLT has the xsl:attribute function to set attributes. Problem solved:

<div class="item"><xsl:attribute name="id">i<xsl:value-of select="@id"/></xsl:attribute><xsl:apply-templates select="@id"/><xsl:apply-templates select="content"/></div>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...