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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

separating XML data into seprate pages - XSLT 1

Status
Not open for further replies.

andyros

Programmer
Jul 12, 2005
42
GB
Hey guys

sorry for the long title :) I've got an XML data structure as follows:

Code:
<entry>
      <name>New Entry</name>
      
       <dataSet1>
            <showingResults>1-5</showingResults
                 <result>
                      <docTitle>April 2005</docTitle>
                      <docPath>c:/april2005.pdf</docPath>
                 </result>
                 <result>
                      <docTitle>March 2005</docTitle>
                      <docPath>c:/march2005.pdf</docPath>
                 </result>
                 ...more <result> entries
       </dataset1>

       <dataset2>
                ... same structure as above, diffrent values
       </dataset2>
</entry>

Is there anyway through XSLT I can create hyperlinks to the diffrent 'datasets'?

For example, my XSLT generates 2 hyperlinks, one for dataset1 and one for dataset2. When i click a hyperlink it changes a div or iframe (something like that) to display the relevant set.

Just wondering if this is possible before i attempt it by a delve into JavaScript :!

thanks
andy

 
It is possible for XSLT to generate the hyperlinks, but you'll need to use javascript to actually hide/show the divs:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
        <script type="text/javascript">
  function show(dataSet)
  {
    for (var i = 1; i &lt; <xsl:value-of select="count(entry/dataSet) + 1"/>; i++)
    {
      document.getElementById("dataSet" + i).style.display = "none";
    }
    document.getElementById("dataSet" + dataSet).style.display = "block";
  }
        </script>
      </head>
      <body>
        <xsl:apply-templates select="entry"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="entry">
    <h1>
      <xsl:value-of select="name"/>
    </h1>
    <ul>
      <xsl:apply-templates select="dataSet" mode="links"/>
    </ul>
    <xsl:apply-templates select="dataSet" mode="divs"/>
  </xsl:template>
  <xsl:template match="dataSet" mode="links">
    <li>
      <a href="#" onclick="javascript:show({position()});">
        <xsl:value-of select="concat('Show dataSet ', position())"/>
      </a>
    </li>
  </xsl:template>
  <xsl:template match="dataSet" mode="divs">
    <div id="dataSet{position()}" style="display: none;">
      <h2>
        <xsl:value-of select="concat('Showing results: ', showingResults)"/>
      </h2>
      <ul>
        <xsl:apply-templates select="result"/>
      </ul>
    </div>
  </xsl:template>
  <xsl:template match="result">
    <li>
      <a href="{docPath}">
        <xsl:value-of select="docTitle"/>
      </a>
    </li>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
woah :) thanks!

Any chance of a bit of explanation as to whats going on? I've spent all of today trying to figure this out :\
 
What part don't you understand? I renamed the dataSets from dataSet1, dataSet2, .... to just dataSet, dataSet,... as its much easier (and more logical to work with them like this).

Stylesheet basically says: For each 'entry' display a list of links and divs for each dataset in it. For each 'dataSet' display each 'result' as a link in that div.

Then you use styling to hide all the divs to start with. Then javascript function to show the one clicked.

Jon

"I don't regret this, but I both rue and lament it.
 
Yep got it :D thanks alot! As you replied i was actually making it so each dataset was a seprate xml file and i was using a cgi script to pass file names back and forth for displaying in an div [ponder]

cheers, helped me loads!
andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top