user1402867
user1402867

Reputation: 189

multiple html as output and one xsl

I am converting a XML to HTML using XSLT. The output of should have 2 HTML files linked with each other. In the first html there will be a list of data and once I click on a particular data I should get the details of that particular data in other html file. I need to use xslt for this.

I have used Saxon to generate multiple htmls but I am able to perform the linking functionality. Any ideas on how to do this?

My input look like this

<a:file>
<a:id>33</a:id>
<a:name>hello</a:name>
<a:school>mumbai public</a:school>
<a:marks>80</a:marks>
</a:file>

Upvotes: 0

Views: 1211

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

Well Saxon 9 or any XSLT 2.0 processor can generate multiple result documents with one stylesheet, as for linking two documents there is nothing difficult about this, you would simply use an HTML a element with a href attribute linking to the other document.

If you need help on this for a particular data then please post a small but representative XML input sample and also the HTML you want to create.

[edit]

Assuming you have an input document with a couple of those a:file elements and you want to create one main HTML document listing all a:names linking to separate files listing the details you can solve that as follows:

<xsl:template match="/">
  <xsl:apply-templates select="//a:file" mode="doc"/>
  <html>
    <head>
      <title>Example</title>
    </head>
    <body>
      <h1>Data list</h1>
      <ul>
        <xsl:apply-templates select="//a:file"/>
      </ul>
    </body>
  </html>
</xsl:template>

<xsl:template match="a:file">
  <li>
    <a href="{a:id}.html">
      <xsl:value-of select="a:name"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="a:file" mode="doc">
  <xsl:result-document href="{a:id}.html">
    <html>
      <head>
        <title>Details of <xsl:value-of select="a:name"/></title>
      </head>
      <body>
        <table>
          <thead>
            <tr>
             <xsl:apply-templates mode="thead"/>
            </tr>
          </thead>
          <tbody>
            <tr>
             <xsl:apply-templates mode="doc"/>
            </tr>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:result-document>
</xsl:template>

<xsl:template match="a:file/*" mode="doc">
  <td>
    <xsl:value-of select="."/>
  </td>
</xsl:template>

<xsl:template match="a:file/*" mode="thead">
  <th>
   <xsl:value-of select="local-name()"/>
  </th>
</xsl:template>

Untested but should give you an idea. If you need more help then please show more details of your input and wanted output, I had to make up the format of both the main HTML result (used a list there) and the details files (used a table there).

[edit 2] Assuming the complete input sample is

<a:files xmlns:a="http://example.com/a">
<a:file>
<a:id>33</a:id>
<a:name>hello</a:name>
<a:school>mumbai public</a:school>
<a:marks>80</a:marks>
</a:file>
</a:files>

and the complete stylesheet sample is

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:a="http://example.com/a"
  exclude-result-prefixes="a"
  version="2.0">

<xsl:template match="/">
  <xsl:apply-templates select="//a:file" mode="doc"/>
  <html>
    <head>
      <title>Example</title>
    </head>
    <body>
      <h1>Data list</h1>
      <ul>
        <xsl:apply-templates select="//a:file"/>
      </ul>
    </body>
  </html>
</xsl:template>

<xsl:template match="a:file">
  <li>
    <a href="{a:id}.html">
      <xsl:value-of select="a:name"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="a:file" mode="doc">
  <xsl:result-document href="{a:id}.html">
    <html>
      <head>
        <title>Details of <xsl:value-of select="a:name"/></title>
      </head>
      <body>
        <table>
          <thead>
            <tr>
             <xsl:apply-templates mode="thead"/>
            </tr>
          </thead>
          <tbody>
            <tr>
             <xsl:apply-templates mode="doc"/>
            </tr>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:result-document>
</xsl:template>

<xsl:template match="a:file/*" mode="doc">
  <td>
    <xsl:value-of select="."/>
  </td>
</xsl:template>

<xsl:template match="a:file/*" mode="thead">
  <th>
   <xsl:value-of select="local-name()"/>
  </th>
</xsl:template>

</xsl:stylesheet>

then when I use Saxon 9.4 HE from the command line doing e.g. java -jar saxon9he.jar input.xml sheet.xsl -o:result.html I get two result files, the main of course being result.html, the other being 33.html which look as follows:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Example</title>
   </head>
   <body>
      <h1>Data list</h1>
      <ul>
         <li><a href="33.html">hello</a></li>
      </ul>
   </body>
</html>




<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Details of hello</title>
   </head>
   <body>
      <table>
         <thead>
            <tr>

               <th>id</th>

               <th>name</th>

               <th>school</th>

               <th>marks</th>

            </tr>
         </thead>
         <tbody>
            <tr>

               <td>33</td>

               <td>hello</td>

               <td>mumbai public</td>

               <td>80</td>

            </tr>
         </tbody>
      </table>
   </body>
</html>

So that works fine for me, both as far as the number of files is concerned as well as for the linking working inside the browser.

Upvotes: 2

Related Questions