0

New to stackoverflow and XML and XSLT. I am receiving external XML feeds which cannot be edited and only transformed for output using XSLT. For example an XML feed could look like:

<competition name="Comp1">
<event name="test1">

<competitor name="competitor1">
<competitor name="competitor2">
<competitor name="competitor3">

</event>
</competition>

and the standard xsl

<xsl:template match="event">

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

</xsl:template>

will out put the following:

competitor1
competitor2
competitor3

Is there a way to print the name separately? i.e. competitor1 on its own and then where i needed i could print competitor2. is this possible?

My guess would be that i'd have to split up the competitor name but again I can't edit the XML file so not sure what approach to take. And for more bad news, the system that the output is displayed on will not work with html.

Updated

The desired output would be something like:

competitor1              competitor2            competitor3

by doing this i can organize the output the way i need, ie. like above

Really appreciate any help, Thanks in advance

Vedd44
  • 3
  • 2
  • Give an example of your wanted output. – Kristofer Feb 17 '12 at 11:49
  • And you wish it to print what? Each competitors name on a separate line? or? – Kristofer Feb 17 '12 at 11:55
  • Hi Kristofer, getting use to using stackoverflow, i updated the question. Usually i'll get an xml feed with a number of names ranging from 4 to 40, each time i get a new feed, a new layout or design for the output has to be made so ideally by breaking up the attributes individually i could display them within columns etc... Is this possible? Apologies for my severe novice-ness, I'm very new to xsl! – Vedd44 Feb 17 '12 at 12:04

1 Answers1

0

This is just an example to guide you, depending on your output format you might wish to change the

<br/>

line breaks to something else, take a look at this post for examples.

Text output formatting(spaces/tabs/linebreaks etc) is always a bit complicated to get right so be prepared to spend some time with it.

Given the XML:

<competition name="Comp1">
  <event name="test1">
    <competitor name="competitor1"/>
    <competitor name="competitor2"/>
    <competitor name="competitor3"/>
  </event>
  <event name="test2">
    <competitor name="competitorX"/>
    <competitor name="competitorY"/>
    <competitor name="competitorZ"/>
  </event>
</competition>

With the following xslt:

<?xml version="1.0"?>  
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
<xsl:apply-templates/>
  </xsl:template>

<xsl:template match="competition/event">
    <xsl:value-of select="@name" />
    <br/>
    <xsl:apply-templates/>
    <br/>
</xsl:template>

<xsl:template match="competitor">
      <xsl:value-of select="@name" />
      <xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>

Will give you the following output:

test1
<br/>
competitor1 competitor2 competitor3
<br/>
test2
<br/>
competitorX competitorY competitorZ
<br/>
Community
  • 1
  • 1
Kristofer
  • 3,201
  • 23
  • 28
  • Cheers for the help @Kristofer, It certainly does require some amount of time but using the code above I should be able to get something done, appreciate it man! – Vedd44 Feb 17 '12 at 13:43