Valhalla Legends Forums Archive | Web Development | [XSL] Writing Attribute Values

AuthorMessageTime
Myndfyr
Hi,

I'm working on creating a table-of-contents via XML and XSL-Transform similar to what you see here.  The problem I'm having is that when I try to generate the HTML table of contents, I need to emit a position() call, for example:

[code]
<xsl:apply-templates match="position()" />
[/code]

That in itself is not difficult.  The problem is that I want the position to be within an attribute.  I could say something along the lines of:

[code]
<a href="#group_<xsl:apply-templates match="position()" />"><xsl:apply-templates match="Name" /></a>
[/code]

But that's not well-formed XML (the < within the attribute value in the <a> tag).  So now you have an idea about what I want to do, but I just don't know how to get there.

I have to do a similar thing later when I want to set up the <a name=> part.

Any experience here?  If not I'll keep playing around; that's how I've gotten my XSL thus far.  :)
November 10, 2004, 3:08 AM
Myndfyr
I figured it out.  Whatever element you want to write an attribute to, you declare it and within nest an <xsl:attribute> element.  For example, I had a couple XSL variables declared, named positionLarge and positionSmall, respectively.  I wanted to generate <a href="#grouppositionLarge_positionSmall" class="nav">NameOfGroup</a> in the output, so:

[code]
<a class="nav">
  <xsl:attribute name="href">#group<xsl:value-of select="format-number($positionLarge, '#')" />_<xsl:value-of select="format-number($positionSmall, '#')" /></xsl:attribute>
  <xsl:apply-templates select="Name" />
</a>
[/code]

positionLarge increments more slowly (on the outside loop) than positionSmall, so it generated something like:
[code]
<a class="nav" href="#group1_1">Beginnings</a>
<a class="nav" href="#group1_2">The Council of Chaos</a>
<a class="nav" href="#group1_3">The Lean Times</a>
<a class="nav" href="#group2_1">Division Within</a>
[/code]
etcetera.

I may write an introductory tutorial to XSL, because I'm finding it so damn useful.  :)
November 11, 2004, 10:49 AM
St0rm.iD
Argh...can you please explain what it's useful for?
November 11, 2004, 6:19 PM
Myndfyr
[quote author=Banana fanna fo fanna link=topic=9491.msg88468#msg88468 date=1100197169]
Argh...can you please explain what it's useful for?
[/quote]

It's useful for transforming raw XML data like you see here into browser-displayable HTML like you see here.

Actually in the XSL version, I don't have attributes in the XML.  But from any page from now on, all I need to do is slightly modify my stylesheet and and it will adapt all the data, as opposed to, when making major layout changes, having to completely rewrite or recode entire paragraphs.
November 11, 2004, 7:42 PM
St0rm.iD
That's terrible! Isn't that what...HTML+RDBMS is for?
November 11, 2004, 8:52 PM
Myndfyr
[quote author=Banana fanna fo fanna link=topic=9491.msg88516#msg88516 date=1100206345]
That's terrible! Isn't that what...HTML+RDBMS is for?
[/quote]

But why would I waste expensive RDBMS calls when I can just store the large text data in an easily-accessible format?  The History page isn't going to be changing that often, so why store static content on a (possibly) remote server?
November 12, 2004, 12:28 AM
St0rm.iD
Hrm. I was under the impression you were querying the XML on each hit.

Still...when I see you've defined a custom paragraph tag, I get worried.
November 12, 2004, 1:24 AM
Myndfyr
[quote author=Banana fanna fo fanna link=topic=9491.msg88549#msg88549 date=1100222689]
Hrm. I was under the impression you were querying the XML on each hit.
[/quote]

I am querying the XML on each hit.  But XML + XML = faster than retrieve DB records + iterate over DB records. :P
November 12, 2004, 2:20 AM
St0rm.iD
I don't believe that.
November 12, 2004, 7:57 PM

Search