Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,
I need your help for invoking anchor tags using xslt. I have all my footnotes contents below in my xml file and they are numbered. These numbers will also be in the main content of my xml file. So when i click on the number inside content, it should take me to the respective footnote number listed below in xml file.

Does anyone came across such case?

If so, kindly let me know or route me to the related article to get some knowledge.

Thanks in advance.
HTML
xml file sample:
<Text>
<Section>
 <Clause>
  <Para>
   <Para>
    <Footnote> [1] </Footnote>
   </Para>
  </Para>
 </Clause>

 <Explanation>
  <Footnote> [2] </Footnote> 
 </Explanation>
</Section>

<Footnotes>
 <Link>[1]</Link>
 <Linkcont>
 	<Foot1>dated January 6, 1994 : </Foot1>
 </Linkcont>
 <Link>[2]</Link>
 <Linkcont><Foot2>Inserted by Act </Foot2></Linkcont>
</Footnotes>
</Text>

I should write xslt in the following manner. Footnote content will be displayed above and footnotes contents will be displayed below the page.
[1]
[2]


[1]
dated January 6, 1994 :
[2]
Inserted by Act.

if i click on [1] above it should take me to the bottom of my screen that is footnotes content. [1] dated January 6, 1994 :
Similarly if i click on [2] it should take me to footnotes [2] Inserted by Act.
Hope this is clear, if not please let me know to make it further more easier to understand.
Posted
Updated 25-Jun-12 1:10am
v4
Comments
Andreas Gieriet 25-Jun-12 4:02am    
Please give some sample files.
You have XML plus XSLT that transforms XML to HTML, I guess. Correct?
Cheers
Andi
rahulaaditya 25-Jun-12 6:34am    
Exactly.
rahulaaditya 25-Jun-12 7:22am    
Sir do you have any idea, plz let me know, i was stuck with this problem for a long time.

1 solution

1. Write the expected HTML manually.
2. Write an XSLT that converts the XML into that HTML
Sounds quite simple and straight forward, I think.
If you struggle with writing the expected HTML, ask a respective question.
If you struggle with generating the anchor name, look at the generate-id()[^] function.

BTW: your XML is badly structured for my taste. If you have that under control, I would strongly suggest to get a better structure (i.e. so that you can use the link id instead of generate-id() function to make the links), e.g.

XML
<?xml version="1.0" encoding="utf-8"?>

<Text>
  <Section>
    <Clause>
      <Para>
        Some <Footnote Ref="1">text</Footnote> and so on.
      </Para>
    </Clause>

    <Explanation>
      Some other <Footnote Ref="2">text</Footnote> etc.
    </Explanation>
  </Section>

  <Footnotes>
    <Note Id="1">dated January 6, 1994 :</Note>
    <Note Id="2">Inserted by Act.</Note>
  </Footnotes>
</Text>


With the following XSLT, you get a linked HTML as shown further down:

XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html>
      <head><xsl:call-template name="SetStyles"/></head>
      <body>
        <xsl:call-template name="WriteText"/>
        <xsl:call-template name="WriteFootnotes"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Footnote">
    <xsl:variable name="Id" select="@Ref"/>
    <xsl:variable name="Link" select="concat('#', $Id)"/>
    <xsl:variable name="Tip" select="/Text/Footnotes/Note[@Id=$Id]"/>
    <xsl:value-of select="."/>
    <sup><a class="footnoteref" href="{$Link}" title="{$Tip}"><xsl:value-of select="concat('[',$Id,']')"/></a></sup>
  </xsl:template>

  <xsl:template match="Para">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <!-- main functions -->
  <xsl:template name="SetStyles">
    <style type="text/css">
      <xsl:comment>
        .footnote { font-size:8pt; }
        .footnoteref { text-decoration:none; color:black;}
      </xsl:comment>
    </style>
  </xsl:template>

  <xsl:template name="WriteText">
    <xsl:for-each select="/Text/Section">
      <xsl:apply-templates/>
    </xsl:for-each>

  </xsl:template>
  <xsl:template name="WriteFootnotes">
    <xsl:if test="count(/Text/Footnotes/Note) != 0">
      <hr/>
      <table border="0" class="footnote">
        <xsl:for-each select="/Text/Footnotes/Note">
          <xsl:sort select="@Id"/>
          <xsl:variable name="Id" select="@Id"/>
          <tr>
            <td><a name="{$Id}"><xsl:value-of select="concat('footnote [', $Id, ']: ')"/></a></td>
            <td><xsl:value-of select="."/></td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>



<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css"><!--
.footnote { font-size:8pt; }
.footnoteref { text-decoration:none; color:black;}
--></style>
</head>
<body>

<p>
Some text<sup><a class="footnoteref" href="#1" title="&#xA; dated January 6, 1994 :&#xA; ">[1]</a></sup> and so on.
</p>



Some other text<sup><a class="footnoteref" href="#2" title="&#xA; Inserted by Act.&#xA; ">[2]</a></sup> etc.

<hr>
<table border="0" class="footnote">
<tr>
<td><a name="1">footnote [1]: </a></td>
<td>
dated January 6, 1994 :
</td>
</tr>
<tr>
<td><a name="2">footnote [2]: </a></td>
<td>
Inserted by Act.
</td>
</tr>
</table>
</body>
</html>


Cheers
Andi
 
Share this answer
 
v2
Comments
rahulaaditya 26-Jun-12 0:00am    
Thank you very much for sharing your ideas. I believe this will open a gate for my way. Let me try it.
rahulaaditya 26-Jun-12 2:40am    
Hi Andi, can i have your email id for sending my file? Bcos i cannot post it here the whole file. I still have some confusion in writing my xslt. My id is raadity@gmail.com
Andreas Gieriet 26-Jun-12 4:58am    
You find my email address through my company's web page as shown in my profile.

But I think for the benefit of everybody, you should take the time to have the relevant XML and XSLT data posted here.

An important information from your side is missing: is the XML structure under your control (i.e. you can define the structure yourself) or is it given. If it is given, you are doomed to do some unpleasant tweeking. If you could change the XML structure to something more suitable (e.g. as shown in my solution), then things become simpler. What is your situation regarding this?

Cheers
Andi
rahulaaditya 3-Jul-12 4:34am    
Dear sir,
Is it possible to print contents in color based on a attribute? Say for eg., if i have a attribute change and its value is Omitted then my contents should get printed in red color font. Otherwise black color.

Sample file:
<yearsuffix value="2011"><Title>67 Method
of computing a partner’s share in the income of the firm</Title><text><Para><omitted>[Omitted by the Finance Act, 1992, with effect from
1-4-1993.]</Para>
Andreas Gieriet 3-Jul-12 8:43am    
What exactly do you mean by "print"? Write colored text in the browser based on some data is no problem: <xsl:choose>....
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900