How do I display this XML document?

XML Help

  • 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.

  • Answer:

    http://www.dpawson.co.uk/xsl/sect2/sect21.html 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.

codacorolla at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

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.

holloway

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.

codacorolla

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.

holloway

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.