Jump to content

Does My Xsl Look Good To You?


hpdp
 Share

Recommended Posts

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 ishttps://docs.google.com/Doc?docid=0ARrdEE8A...Gpkbg&hl=enThe link for the html output ishttps://docs.google.com/Doc?docid=0ARrdEE8A...zMybg&hl=enThanks a trillion!

Link to comment
Share on other sites

You don't need to have

<xsl:template match="//kwd-group/kwd">

unless perhaps you also have kwd elements as childs of other elements and want to do something different for them.You can simply have

<xsl:template match="kwd">

And the same goes for pretty much your whole stylesheet.Also, when you have a template in place, there's no need to use for-each. For example, you can reaplce

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

with

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

unless perhaps you want two extra br elements (for a total of 4?), in which case a combination of both is the most elegant solution:

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

As for how to create links, I'm not sure I get it... where must each link be? What should its content depend on?

Link to comment
Share on other sites

Thank you for all the helpful suggestions. The link in the html output should be the first fine:Abstract Materials Results DiscussionEach 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. 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!!

Link to comment
Share on other sites

Well, that's one more reason to scrap all "//" in front of the template match. You're executing it too many times (or you're only getting one match? I'm not sure... your XML is somewhat messy).Replace

<xsl:template match="//body/sec">

with

<xsl:template match="body/sec">

or better yet (if "sec" only appears in "body")

<xsl:template match="sec">

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...