XML Help
November 16, 2010 12:05 PM   Subscribe

I'm sort of stuck on trying to use an XML document to define the SRC properties for images...

Let's say that I'm building an XML stylesheet for a catalog system that has XML entries like this (subbing [ for brackets)
[item]
[title]burning chrome[/title]
[author]william gibson[/author]
....
[imagesource]cover1.jpg[/imagesource]
[/item]
And an xml stylesheet similar to this:
http://pastebin.com/gQhtsGri

I've Googled and found tutorials, but they're all going over my head. Can anyone help?

Also, if you know of any good XML tutorials online could you link those? I find the W3C a little skimpy. I'm also sort of stumped on using conditionals in a stylesheet to display dynamic content, but that seems like another question unto itself.
posted by codacorolla to Computers & Internet (4 answers total) 1 user marked this as a favorite
 
Dave Pawson's FAQ is a great XSL-T reference.

If your question is how you map a text node to an attribute, then you you've got two options.

1) Shorthand inline syntax,

<img src="{XPATH}"/>

the {} brackets tell your XSL-T processor to interpret it as an expression, not a literal string.

2) Conventional attribute syntax,

<img>
<xsl:attribute name="src"><xsl:value-of select="XPATH"/></xsl:attribute>
</img>


...where XPATH in both examples is replaced with your xpath reference to the node you're replacing. Eg, It could be item/imagesource depending on the context.

It's kind of difficult to know what stage you're at in learning XSL-T though from what you've said so far so if you're got more questions then fire away.
posted by holloway at 12:16 PM on November 16, 2010


By the way... you've got this in your template,

<table width="40%" border="0" bgcolor="white" cellpadding="0" cellspacing="0">
<xsl:apply-templates select="catalog/item">
<xsl:sort select="title" />
</xsl:apply-templates>
</table>


So you're outputting a table and applying templates on the item element, ok, but then...

<xsl:template match="item">
<p>
...


Which means the end result will (in part) look like,

<table width="40%" border="0" bgcolor="white" cellpadding="0" cellspacing="0">
<p>
...


but you'd want some <tr>/<td>s in there first. You might want to consider ditching the HTML 3.2 style stuff you've got there too like <font> and bgcolor and instead use <span>s and style.
posted by holloway at 12:27 PM on November 16, 2010


Response by poster: Aren't the table tags handled in each different cell of the page? Like here:

[xsl:template match="title"]
[tr]
[td bgcolor="#FBEC5D"]
[font class="titlehead"]
Title:
[xsl:value-of select="." /]
[/font]
[br /]
[/td]
[/tr]
[/xsl:template]

I mean... it renders like I'd expect, so I'm not too worried about that.

HTML 3.2 tags you can blame on me not designing sites for the past 4 years or so :)

Also: how are you putting bracketed code into your posts? A quick search on the faq recommended pastebin, which isn't practical for smaller stuff.
posted by codacorolla at 12:39 PM on November 16, 2010


Replace your 'less than' and 'greater than' tags with &lt; and &gt; respectively to make them visible as text nodes. Read up on XML entities.
Aren't the table tags handled in each different cell of the page?
No, you're applying templates on item not title so according to your pastebin the <xsl:template match="item"> will have precedence and that's outputting a <p> where it shouldn't be.

It's only when the template reaches the bold line below...
<xsl:template match="item">
<p>
<xsl:apply-templates select="title" />
...


...that the <xsl:template match="title"> template takes control.
posted by holloway at 12:53 PM on November 16, 2010


« Older Fictional Characters Who Need Therapy   |   Looking for an online music service... Newer »
This thread is closed to new comments.