Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Could someone help me with XSLT??

Status
Not open for further replies.

Bloobird83

Technical User
Jan 20, 2005
2
GB
Hi I am using XML Spy 2005. I have been given the task of creating an XML source, and then generating HTML pages from it using XSLT.

The subject is a general store that sells PCs, games and Phones.

I am currently trying to get the basic naviagation working.

Below is some of the XML:


<catalogue>

<catagory>
<title>Hardware</title>
<link>Hardware.html</link>
<subcatagory>Desktops</subcatagory>
<subcatagoryURL>Desktops.html</subcatagoryURL>
<subcatagory>Laptops</subcatagory>
<subcatagoryURL>Laptops.html</subcatagoryURL>
<subcatagory>Workstations</subcatagory>
<subcatagoryURL>Workstations.html</subcatagoryURL>
<subcatagory>Servers</subcatagory>
<subcatagoryURL>servers.html</subcatagoryURL>
</catagory>

</catalogue>



What I would like it to do is generate a page for each subcatagory with the subcatagory as the URL.
E.g. address/hardware/desktops.html.


On each page however I would like links to the other subcatagories. So if you were on desktops you could click on laptops, servers etc...

I have been working on the XSLT file for a while but get the same problem! All of the hyperlinks show up, but when i mouse over them I can see that the link says
laptop.html workstaion.html and so on.... all on one line. So im guessing I need to put another loop somewhere! So it will pick out these urls and put them on the appropriate place!

Anyway here is the section of XSLT code ive been using:


<xsl:for-each select="catalogue/catagory">
<xsl:result-document href="{title}{'.html'}">
<a>
<xsl:attribute name="href">
<xsl:value-of select="subcatagoryURL"></xsl:value-of>
</xsl:attribute>
<xsl:value-of select="subcatagory"></xsl:value-of>
</a>
</xsl:result-document>
</xsl:for-each>


Any help on this would be awesome!!
I can send the full XML and XSL file if needed!

Thanks

James!!
 
The trick is in the {} shortcut...
Code:
<a href="{subcatagoryURL}"><xsl:value-of select="subcatagory" /></a>

I have yet to get <xsl:attribute> work like it is supposed to...

Why do you have '.html' in brackets?
<xsl:result-document href="{title}{'.html'}">

does that work like you intended? or should it be:
<xsl:result-document href="{title}'.html'">
or:
<xsl:result-document href="{title} + '.html'">

And the biggest problem is your XML...

You need to split your subcat... items into sub nodes...

Code:
<catalogue>
<catagory>
   <title>Hardware</title>
   <link>Hardware.html</link>
    <subcatagory>Desktops</subcatagory>
    <subcatagoryURL>Desktops.html</subcatagoryURL>
    <subcatagory>Laptops</subcatagory>
    <subcatagoryURL>Laptops.html</subcatagoryURL>
    <subcatagory>Workstations</subcatagory>
    <subcatagoryURL>Workstations.html</subcatagoryURL>
    <subcatagory>Servers</subcatagory>
    <subcatagoryURL>servers.html</subcatagoryURL>
</catagory>
</catalogue>

should be something like:
Code:
<catalogue>
  <title>Hardware</title>
  <link>Hardware.html</link>
  <catagory>
    <subcatagory>Desktops</subcatagory>
    <subcatagoryURL>Desktops.html</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Laptops</subcatagory>
    <subcatagoryURL>Laptops.html</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Workstations</subcatagory>
    <subcatagoryURL>Workstations.html</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Servers</subcatagory>
    <subcatagoryURL>servers.html</subcatagoryURL>
  </catagory>
</catalogue>

Then your xsl should look something like this::
Code:
<xsl:for-each select="//catagory">
  <a href="{subcatagoryURL}"><xsl:value-of select="subcatagory"></xsl:value-of></a><br />
</xsl:for-each>


Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Ok thanks very much for your help.
There is one thing. Hardware is not the only catagory in the store. So do you know how I could output for example, Phones and Phone links on one page and then maybe Games and game links on another page?
The link below show what im trying to get


Thank you very much!
 
well... If you are trying to set it up to use XML w/ XSL instead of html...

There are several things you can do...

First...

All the .html in your code above need to be .xml
...
Code:
<catalogue>
  <title>Hardware</title>
  <catagory>
    <subcatagory>Desktops</subcatagory>
    <subcatagoryURL>Desktops.xml</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Laptops</subcatagory>
    <subcatagoryURL>Laptops.xml</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Workstations</subcatagory>
    <subcatagoryURL>Workstations.xml</subcatagoryURL>
  </catagory>
  <catagory>
    <subcatagory>Servers</subcatagory>
    <subcatagoryURL>servers.xml</subcatagoryURL>
  </catagory>
</catalogue>

Fhen update the rest of them the exact same way...

From there, you can either setup a seperate XSL for each style of page (like I have burried in the link on my site) or set up 1 XSL file to handle everything...

how do you do this?

With the <xsl:if> and/or <xsl:choose> tag(s)...

Design your site to have a special, unique, keyword for each common TYPE of data...

The cool thing about XML, is that you don't have to have identical sub elements on element... So you can get as creative as you like, then style your XSL around it...

Say you have a file like:
Code:
<test>
  <menu>
    <title>Menu 1</title>
    <item>
      <name>Test 1</name>
      <link>test1.xml</link>
    </item>
    <item>
      <name>Test 2</name>
      <link>test2.xml</link>
    </item>
    <item>
      <name>Test 3</name>
      <link>test3.xml</link>
    </item>
  </menu>
</test>

And another such as:
Code:
<test>
  <content>
    <article>
      <Title>Article 1</Title>
      <Author>Josh</Author>
      <Text>The text goes here...</Text>
    </article>
    <article>
      <Title>Article 2</Title>
      <Author>Josh</Author>
      <Text>Other text goes here...</Text>
    </article>
  </content>
</test>

You could display both of them with the same XSL...
Code:
<xsl:template match="/">
  <html>
  <xsl:if select="title">
    <head><title><xsl:value-of select="title" /></title></head>
  </xsl:if>
  <body>
  <xsl:if test="//menu">
    <table>
    <xsl:for-each select="//menu/item">
      <tr>
      <td><a href="{link}"><xsl:value-of select="name" /></a></td>
      </tr>
    </xsl:for-each>
    </table>
  </xsl:if>
  <xsl:if test="//content">
    <table>
    <xsl:for-each select="//article/">
      <tr>
        <td><xsl:value-of select="Title" /></td>
        <td>By: <xsl:value-of select="Author" /></td>
      </tr>
      <tr>
        <td colspan="2"><xsl:value-of select="Text" /></td>
      </tr>
    </xsl:for-each>
    </table>
  </xsl:if>
  </body>
  </html>
</xsl:template>

That might give you an idea or 2 for designing a site...

Hope This Helps ;-)
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top