Re: [xsl] Embedded bold,italic,anchors etc.

Subject: Re: [xsl] Embedded bold,italic,anchors etc.
From: Craig Riley <craig@xxxxxxxxxxxxxxxxxx>
Date: Wed, 11 Jun 2008 11:10:05 -0400
Wow some great responses (and the usual not so great ones :)

The reason I did not supply any code is because a) This was one of the
first things I tried to do and because it didn't work I deleted the
code, b) the code I have now is quite large and c) I just wanted a
nudge in right direction so I wrote an example peice of code out.

From what I have read I was probably using the value-of instead of
the recursive technique somebody suggested. For what it's worth this
is the code I have to date, I'm sure there's tons of things wrong with
it but that's part of learning! I can already see I'm not using
templates nowhere near enough! :(

Thanks again for the helpful emails

Cheers

Craig







<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output omit-xml-declaration="yes" />

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

    <xsl:template match="*/italic">
        <span style="color:#FF00FF">
          <xsl:apply-templates/>
        </span>
    </xsl:template>


<!-- Front Node --> <xsl:template match="/article/front">

        <div style="width:80%; text-align:left; margin:0px auto;
font-family:Arial, Helvetica, sans-serif;">

        	<!-- Header -->
            <div style="color:#999999; font-size:18; text-align:center">
                <xsl:text>Publication: </xsl:text><xsl:value-of
select="journal-meta/journal-title" /><xsl:text>; </xsl:text>
                <xsl:text>Type: </xsl:text><xsl:value-of
select="article-meta/article-categories/subj-group/subject" />
                <br />
                <xsl:text>DOI: </xsl:text><xsl:value-of
select="article-meta/article-id" /><xsl:text>; </xsl:text>
                <xsl:text>Volume: </xsl:text><xsl:value-of
select="article-meta/volume" /><xsl:text>; </xsl:text>
                <xsl:text>Issue </xsl:text><xsl:value-of
select="article-meta/issue" />
            </div>

            <!-- Title -->
            <h1 style="font-size:30; color:#000000; text-align:left">
                <xsl:value-of
select="article-meta/title-group/article-title" />
            </h1>

            <!-- We need to compare total contribs to current contrib
so we don't have a comma at end of list -->
            <xsl:variable name="totalContribs"
select="count(article-meta/contrib-group/contrib)" />
            <!-- Contributers -->
            <xsl:for-each select="article-meta/contrib-group/contrib">

                <!-- Contrib Full Name-->
            	<xsl:value-of select="name/given-names" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="name/surname" />

                <!-- We need to compare total sups to current sup so
we don't have a comma at end of list -->
                <xsl:variable name="totalSups" select="count(xref/sup)" />
                <xsl:for-each select="xref">

                    <!-- Get current sup count position -->
                    <xsl:variable name="currentSup" select="position()" />

                    <sup>
                    	<xsl:value-of select="sup" />
                        <!-- If current position not equal to total
the display comma -->
                        <xsl:if test="$currentSup &lt; $totalSups">
                            <xsl:text>,</xsl:text>
                        </xsl:if>
                    </sup>

</xsl:for-each>

<xsl:value-of select="role" />

                <!-- Get current sup count position -->
                <xsl:variable name="currentContrib" select="position()" />

                <!-- If current position not equal to total the
display comma -->
                <xsl:if test="$currentContrib &lt; $totalContribs">
                    <xsl:text>, </xsl:text>
                </xsl:if>

            </xsl:for-each>
           	<!-- affs -->
            <xsl:for-each select="article-meta/contrib-group/aff">
            	<p><xsl:value-of select="." /></p>
            </xsl:for-each>
           	<!-- Author Notes -->
            <xsl:for-each select="article-meta/author-notes/corresp">
            	<p><xsl:value-of select="." /></p>
            </xsl:for-each>


<!-- Abstract Block --> <h1><xsl:value-of select="article-meta/abstract/title" /></h1>

            <xsl:for-each select="article-meta/abstract/p">
            	<p><xsl:value-of select="." /></p>
            </xsl:for-each>

</div>

</xsl:template>


<!-- Body Node --> <xsl:template match="/article/body/sec">

    	<div style="width:80%; text-align:left; margin:0px auto;
font-family:Arial, Helvetica, sans-serif;">

            <!-- Main Copy -->
            <h1 style="margin-top:25px"><xsl:value-of select="title" /></h1>
            <xsl:for-each select="p">
            	<p><xsl:value-of select="." /></p>
            </xsl:for-each>

            <xsl:for-each select="sec">
            	<h2><xsl:value-of select="title" /></h2>
                <xsl:for-each select="p">
                    <p><xsl:value-of select="." /></p>
                </xsl:for-each>
        	</xsl:for-each>


<!-- Tables --> <xsl:for-each select="table-wrap">

                <br />
            	<p><b><xsl:value-of select="label" /></b><xsl:value-of
select="caption/title" /></p>
                <xsl:for-each select="table">
                	<table border="0" cellpadding="3" cellspacing="0"
bgcolor="#666666" width="90%">
                    <xsl:variable name="totalCols"
select="count(thead/tr/th)" />
                    <xsl:for-each select="thead/tr">
                    	<tr bgcolor="#FFFFFF">
                        <xsl:for-each select="th">

                            <xsl:variable
name="colspan"><xsl:value-of select="@colspan" /></xsl:variable>
                            <xsl:variable name="align"><xsl:value-of
select="@align" /></xsl:variable>
                            <xsl:variable name="valign"><xsl:value-of
select="@valign" /></xsl:variable>
                        	<!--
                            <xsl:param name="colspan"
select="1"></xsl:param>
                   			<a href="{concat($URL, $ID,
'&amp;dopt=Abstract')}">Medline</a>
                            -->
                            <th colspan="{$colspan}" align="{$align}"
valign="{$valign}"><xsl:value-of select="." /></th>
                        </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                    <tr bgcolor="#FFFFFF"><td
colspan="{$totalCols}"><hr style="color:#000000; height:1px"
/></td></tr>
                    <xsl:for-each select="tbody/tr">
                    	<tr bgcolor="#FFFFFF">
                        <xsl:for-each select="td">

                            <xsl:variable
name="colspan"><xsl:value-of select="@colspan" /></xsl:variable>
                            <xsl:variable name="align"><xsl:value-of
select="@align" /></xsl:variable>
                            <xsl:variable name="valign"><xsl:value-of
select="@valign" /></xsl:variable>

                        	<td colspan="{$colspan}" align="{$align}"
valign="{$valign}"><xsl:value-of select="." /></td>

                        </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                    </table>
                    <br /><br />
                </xsl:for-each>


</xsl:for-each>


</div>

</xsl:template>


<!-- Back Node --> <xsl:template match="/article/back">


<div style="width:80%; text-align:left; margin:0px auto; font-family:Arial, Helvetica, sans-serif;">

            <!-- Acknowledgements -->
            <xsl:for-each select="ack">
            	<h3 style="margin-top:25px"><xsl:value-of select="title"
/></h3>
                <xsl:for-each select="p">
                    <p><xsl:value-of select="." /></p>
                </xsl:for-each>
        	</xsl:for-each>

            <!-- References -->
            <h3 style="margin-top:25px"><xsl:value-of
select="ref-list/title" /></h3>
            <xsl:for-each select="ref-list/ref">
            	<p>

<xsl:value-of select="label" /><xsl:text>. </xsl:text>

<xsl:choose>

                    <!-- journal -->
                    <xsl:when test="citation[@citation-type='journal']">

                        <xsl:for-each select="citation/person-group/name">
                            <span>
                                <xsl:value-of select="surname" />
                                <xsl:text> </xsl:text>
                                <xsl:value-of select="given-names" />
                                <xsl:text>, </xsl:text>
                            </span>
                        </xsl:for-each>
                        <xsl:text>et al. </xsl:text>
                        <xsl:value-of select="citation/article-title"
/><xsl:text> </xsl:text>
                        <span style="font-style:italic"><xsl:value-of
select="citation/source" /></span><xsl:text> </xsl:text>
                        <xsl:value-of select="citation/year"
/><xsl:text>; </xsl:text>
                        <xsl:value-of select="citation/volume" />
                        <xsl:text>:</xsl:text>
                        <xsl:value-of select="citation/fpage" />
                        <xsl:text>-</xsl:text>
                        <xsl:value-of select="citation/lpage" />
                        <xsl:text>. </xsl:text>

                  	</xsl:when>
                    <!-- book -->
                    <xsl:when test="citation[@citation-type='book']">
                    	<xsl:value-of select="citation" />
                    </xsl:when>
                    <!-- thesis -->
                    <xsl:when test="citation[@citation-type='thesis']">
                    	<xsl:value-of select="citation" />
                    </xsl:when>
                    <!-- web -->
                    <xsl:when test="citation[@citation-type='web']">
                    	<xsl:value-of select="citation" />
                    </xsl:when>
                    <!-- confproc -->
                    <xsl:when test="citation[@citation-type='confproc']">
                    	<xsl:value-of select="citation" />
                    </xsl:when>
                    <!-- No default? -->
                    <xsl:otherwise>
                    	<xsl:value-of
select="citation[@citation-type='other']" />
                    </xsl:otherwise>
                </xsl:choose>

                <!-- Medline Links -->
                <xsl:for-each select="citation/pub-id[@pub-id-type='pmid']">

                    <xsl:variable
name="URL">http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&amp;db=PubMe
d&amp;list_uids=</xsl:variable>
                    <xsl:variable name="ID"><xsl:value-of select="."
/></xsl:variable>
                    <a href="{concat($URL, $ID,
'&amp;dopt=Abstract')}">Medline</a>

</xsl:for-each>

                <!-- DOI Links -->
                <xsl:for-each select="citation/pub-id[@pub-id-type='doi']">

                    <xsl:variable
name="DOIURL">http://dx.doi.org/</xsl:variable>
                    <xsl:variable name="DOIID"><xsl:value-of
select="." /></xsl:variable>
                    <xsl:text> </xsl:text>
                    <a href="{concat($DOIURL,
$DOIID)}"><xsl:text>doi:</xsl:text><xsl:value-of select="." /></a>

                </xsl:for-each>
                </p>

</xsl:for-each>

</div>

</xsl:template>

</xsl:transform>










Craig Riley wrote:

I have created an XSLT style sheet that does everything I need it to
except for handling embedded text. The XML documents I am transforming
contain the usual italic, bold, superscript, plus hyper links that
need converting into html.

Of course I need them to retain there place in the copy of course.

The key to solve that is usually writing templates for the elements you want to process where you make sure the necessary transformation happens and where you use xsl:apply-templates in the body of the template e.g.

  <xsl:template match="foo">
    <strong>
      <xsl:apply-templates/>
    </strong>
  </xsl:template>

  <xsl:template match="bar">
    <em>
      <xsl:apply-templates/>
    </em>
  </xsl:template>



--

	Martin Honnen
	http://JavaScript.FAQTs.com/



---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Current Thread