1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.


December 27, 2007  |  XSL woes  |  1230 hit(s)

One of the problems with using a technology like XSLT only when you need to is that you never get much better, and every new task is a headache. I'm having a transform problem that's stumped me and for now I'm giving up. Maybe you can help.

I have all those quotations (see left, or all of them) in an XML file that looks like this (this is the beginning of the file):
<?xml version="1.0" standalone="yes"?>
<QuotationsTable>
<Quotation>
<quoteId>1</quoteId>
<quote>And so to bed.</quote>
<author>Samuel Pepys</author>
<source />
<tags>
<tag>general</tag>
<tag>literature</tag>
</tags>
</Quotation>
<Quotation>
<quoteId>2</quoteId>
<quote>I have made this letter longer than usual, only
because I have not had the time to make it shorter.</quote>
<author>Blaise Pascal</author>
<source />
<tags>
<tag>writing</tag>
<tag>editing</tag>
</tags>
</Quotation>

<!-- etc. -->
I want to add tags, which as you can see I've added as a <tags> parent with individual <tag> child elements.

To display all of them, I have a page that uses an ASP.NET XmlDataSource control as the data source. A problem with using the XmlDataSource control is that it likes its data exposed as attributes, not elements. I sorted that out some time ago by creating an .xsl file that transformed the each quotation's child elements into attributes, like this:
<xsl:template match="/">
<xsl:for-each select="QuotationsTable">

<xsl:element name="Quotations">

<xsl:for-each select="Quotation">
<xsl:element name="Quotation">

<xsl:attribute name="quoteId">
<xsl:value-of select="quoteId"/>
</xsl:attribute>

<xsl:attribute name="quote">
<xsl:value-of select="quote"/>
</xsl:attribute>

<xsl:attribute name="author">
<xsl:value-of select="author"/>
</xsl:attribute>

<xsl:attribute name="source">
<xsl:value-of select="source"/>
</xsl:attribute>

</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>
Now I want to add the tags. My current attempt looks like this ... I'm looping through the <tags> element and extracting each tag.
<!-- ... -->


<xsl:attribute name="source">
<xsl:value-of select="source"/>
</xsl:attribute>


<xsl:attribute name="tags">
<xsl:for-each select="tags/tag">
<xsl:value-of select="tag" />&amp;nbsp;,
</xsl:for-each>
</xsl:attribute >


<!-- etc. -->

This kind of works, for very loose definitions of "works". When I display the results, I see as many commas as I have tags for each quotation. But I don't see the actual tag values.

I'm stumped, and I don't know how one goes about debugging transforms, as I believe I've whined before. So I guess this will wait until I either am motivated to give it another bash or decide that XML is not the thing for this job. Which would be too bad, kinda.




Phil Weber   27 Dec 07 - 9:21 PM

Hi, Mike: Try this:

<xsl:attribute name="tags">
<xsl:for-each select="tags/tag">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>
</xsl:attribute >


 
mike   28 Dec 07 - 12:19 AM

Wow, Phil, that is so beautiful. Works like a charm. Looks like I needed different syntax for selecting the tag value, and the if test is just a bonus. :-)

Thanks!