Hi, I'm new to xslt. I need to write a xslt on medical journals.
I made it work. But I found if I need to add something later, it's hard to do. Right now, I'm trying to add links to different sections. I know I need to use test on element value. Though I found some sample codes about this, but it doesn't work with my xsl.
And I know there are many ways to write xsl, maybe I'm not doing it in the smart way. Thanks a lot!

Here is my xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="html"/>

    <xsl:template match="/"> 
    <html>
      <body bgcolor="#FFFFFF">
        <a href="#abs">Abstract</a>
        <xsl:text>      </xsl:text>
        <a href="#mat">Materials</a>
        <xsl:text>      </xsl:text>
        <a href="#res">Results</a>
        <xsl:text>      </xsl:text>
        <a href="#dis">Discussion</a>
        <xsl:apply-templates select="//title-group/article-title"/> 
        <xsl:apply-templates select="//contrib/name"/>
        <br/>
        <xsl:apply-templates select="//aff"/> 
        <xsl:apply-templates select="//corresp"/>
        <xsl:apply-templates select="//abstract"/>
        <xsl:apply-templates select="//kwd-group/kwd"/>  
        <xsl:apply-templates select="//body/sec"/>  
        <xsl:apply-templates select="//back/ack"/>
        <xsl:apply-templates select="//back/ref-list"/> 
      </body>
    </html>
    </xsl:template>

    <xsl:template match="//title-group/article-title">
      <br/><h3><xsl:apply-templates/></h3>
    </xsl:template>

    <xsl:template match="//contrib/name">
      <xsl:apply-templates select="given-names"/>  
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="surname"/> 
      <xsl:text>, </xsl:text>
    </xsl:template>

    <xsl:template match="//aff">
      <br/>
      <font color="grey">
        <xsl:apply-templates select="child::text()"/>
      </font>
    </xsl:template>

    <xsl:template match="//corresp">
      <br/>
      <font color="grey">
        <xsl:apply-templates select="label"/>
        <xsl:apply-templates select="child::text()"/>
        <font color="blue"><xsl:apply-templates select="email"/></font>
      </font>
    </xsl:template>

    <xsl:template match="//abstract">
      <h4><center><a name="#abs">Abstract</a></center></h4>
      <xsl:apply-templates/>
      <br/><br/><b>Keywords: </b>
    </xsl:template>
 
    <xsl:template match="//kwd-group/kwd">
      <xsl:apply-templates/> 
      <xsl:text>, </xsl:text>
    </xsl:template> 

    <xsl:template match="//body/sec">
      <center><font color="blue">
      <br/><xsl:apply-templates select="title"/> 
      </font></center>
      <br/>

      <xsl:for-each select="p"> 
        <xsl:apply-templates/>
        <br/><br/>
      </xsl:for-each>

      <xsl:for-each select="sec"> 
        <center><b>
        <br/><xsl:apply-templates select="title"/> 
        </b></center>
        <br/>
        <xsl:apply-templates select="p"/>
        <br/><br/>
      </xsl:for-each> 

      <!-- <xsl:for-each select="descendant::p"> 
        <xsl:apply-templates/>
        <br/><br/>
      </xsl:for-each>  -->

    </xsl:template> 

    <xsl:template match="//back/ack">
      <center><font color="blue">
      <br/><xsl:apply-templates select="title"/> 
      </font></center>
      <br/>
      <xsl:apply-templates select="p"/> 
    </xsl:template>

    <xsl:template match="//back/ref-list">
      <center><font color="blue">
      <br/><xsl:apply-templates select="title"/> 
      </font></center>
      <br/>
      <xsl:for-each select="ref"> 
        <xsl:apply-templates/>
        <br/><br/>
      </xsl:for-each>  
    </xsl:template>

</xsl:stylesheet>

Sorry!. This is long. The link for the xml is
https://docs.google.com/Doc?docid=0ARrdEE8AtkWQZGdramdqZDZfMjJocGp6dGpkbg&hl=en

The link for the html output is
https://docs.google.com/Doc?docid=0ARrdEE8AtkWQZGdramdqZDZfMjNnZnQzdzMybg&hl=en

Thanks a trillion!

Recommended Answers

All 8 Replies

Can you tell us where your links are in the xml and where you want them in the output ? I'm not quite sure what you are trying to achieve.

The link in the html output should be the first fine:

Abstract Materials Results Discussion

Each of the above words links to the section of that page. In my xml, I tried

<xsl:template match="//body/sec">
      <center>
        <font color="blue">
          <br/>
          <xsl:if test="title">
            <xsl:if test=".='MATERIALS AND METHODS'">
              <a name="mat"><xsl:apply-templates select="title"/> </a>
            </xsl:if>
            <xsl:if test=".='RESULTS'">
              <a name="res"><xsl:apply-templates select="title"/> </a>
            </xsl:if>
            <xsl:if test=".='DISCUSSION'">
              <a name="dis"><xsl:apply-templates select="title"/> </a>
            </xsl:if>
            <a name="res"><xsl:apply-templates select="title"/> </a>
          </xsl:if>
        </font>
      </center>
      <br/>

      <xsl:for-each select="p"> 
        <xsl:apply-templates/>
        <br/><br/>
      </xsl:for-each>

      <xsl:for-each select="sec"> 
        <center><b>
        <br/><xsl:apply-templates select="title"/> 
        </b></center>
        <br/>
        <xsl:apply-templates select="p"/>
        <br/><br/>
      </xsl:for-each> 

      <!-- <xsl:for-each select="descendant::p"> 
        <xsl:apply-templates/>
        <br/><br/>
      </xsl:for-each>  -->

    </xsl:template>

But it didn't work. Thanks!!

The '<a name="res"><xsl:apply-templates select="title"/> </a>'
gave name "res" to every section. But if I take that line out, all the section titles are gone.
Thanks!!

When defining a named link, you don't need the #, like this:

<xsl:template match="//abstract">
      <h4><center><a name="abs">Abstract</a></center></h4>
      <xsl:apply-templates/>
      <br/><br/><b>Keywords: </b>
    </xsl:template>

Thanks! That's a mistake. I still have problem with the following part:

<xsl:if test="title">
            <xsl:if test=".='MATERIALS AND METHODS'">
              <a name="mat"><xsl:apply-templates select="title"/></a>
            </xsl:if>
            <xsl:if test=".='RESULTS'">
              <a name="res"><xsl:apply-templates select="title"/> </a>
            </xsl:if>
            <xsl:if test=".='DISCUSSION'">
              <a name="dis"><xsl:apply-templates select="title"/> </a>
            </xsl:if>
            <a name="res"><xsl:apply-templates select="title"/> </a>
</xsl:if>

The '<a name="res"><xsl:apply-templates select="title"/> </a>'
gave name "res" to every section. But if I take that line out, all the section titles are gone.
Thanks!!

I think you mean to do this:

<xsl:choose>
  <xsl:when test="title='MATERIALS AND METHODS'">
    <a name="mat"><xsl:apply-templates select="title"/></a>
  </xsl:when>
  <xsl:when test="title='RESULTS'">
    <a name="res"><xsl:apply-templates select="title"/> </a>
  </xsl:when>
  <xsl:when test="title='DISCUSSION'">
    <a name="dis"><xsl:apply-templates select="title"/> </a>
  </xsl:when>
  <xsl:otherwise>
    <a name="other"><xsl:apply-templates select="title"/> </a>
  </xsl:otherwise>
</xsl:choose>

Thanks a million!! It works now!:)

No problem, glad I could help. Please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.