Jump to content

XSL, y u no let me use <xsl:value-of /> to specify an HTML attribute?


Recommended Posts

I asked about this problem, oh, two years ago, and never got any help on it. Basically gave up on the project. Now, though it's reared its ugly head again. Let's see if I get a response this time. So, have some example XML:

<base> <parent>  <child>blah blah blah</child>  <abcd>efghijkl</abcd></parent> <parent>  <child>oh my</child>  <abcd>mnopqrstuv</abcd></parent> </base>

Now, what I want to do, is use XSL to create, say, this—

<div class="parent">  <p class="child" id="efghijkl">  blah blah blah  </p></div> <div class="parent">  <p class="child" id="mnopqrstuv">  oh my  </p></div>

—from that XML. Here's an example of what I've got so far for XSL:

<xsl:template match="/base"> <xsl:for-each select="parent"><div class="parent">  <p class="child" id="what is this i dont even">  <xsl:value-of select="./child">  </p></div></xsl:for-each> </xsl:template>

So, does anyone have any idea at all as to how in the world I could get the content of <abcd> to be transformed into an id of <div class="child">? Just shoving an <xsl:value-of /> inside the quotation marks doesn't do anything but make browsers regurgitate an error into my face, as occurred in the past, and considering how long this tortured me before I finally relented and just abandoned the code the first time, I'm asking right off the bat.

Edited by BlueDigit
  • Like 1
Link to post
Share on other sites

Hi. I hope this helps. Based on the XML:-

<?xml version = '1.0' encoding = 'ISO-8859-1'?><?xml-stylesheet type="text/xsl" href="Question2.xslt"?><base><parent><child>blah blah blah</child><abcd>efghijkl</abcd></parent><parent><child>oh my</child><abcd>mnopqrstuv</abcd></parent></base>

The XSLT:-

<?xml version = '1.0' encoding = 'ISO-8859-1'?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"><xsl:output method="html"/><xsl:template match="/"><xsl:apply-templates select="base/parent"/></xsl:template><xsl:template match="parent"><div><xsl:attribute name="class"><xsl:value-of select ="local-name()"/></xsl:attribute><xsl:apply-templates select="child"/></div></xsl:template><xsl:template match="child"><p><xsl:attribute name="class"><xsl:value-of select ="name(.)"/></xsl:attribute><xsl:attribute name="id"><xsl:value-of select ="../abcd"/></xsl:attribute><xsl:value-of select ="text()"/></p></xsl:template></xsl:stylesheet>

produced this output in both Opera and Firefox

<html>    <head/>         <body>               <div class="parent">                     <p id="efghijkl" class="child">blah blah blah</p>               </div>               <div class="parent">                     <p id="mnopqrstuv" class="child">oh my</p>               </div>        </body></html>

The functions select ="local-name()" select ="name(.)" produce the same output which is the name of the current XPath node.The XSL element xsl:attribute, as the name suggests inserts an attribute into the current tag. In this case following the syntax <xsl:attribute name="class"><xsl:value-of select ="local-name()"/></xsl:attribute>

Edited by dies_felices
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...