Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2007, 03:17 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to handle several attributes?

Hi,
I'm afraind I'm probably going to ask a very basic question - but my XSLT-experience so far consists of a few hours fiddling with a stylesheet I have inherited and to which I just need to make a few changes to get it to do the work that needs to be done rather soon. ;) I hope you don't mind - I *will* look into this in much more detail once this week's madness is over...

Anyway - I have an xml-file that needs to be converted to HTML for display in a standard browser. I have successfully used xsltproc to do this, but I need to make some more elaborate additions that depend on the information contained in several attributes of a tag. For example:


<head rend="bo" type="MAIN">
<s n="13"><w c5="VDD" hw="do" pos="VERB">Did </w><w c5="PNP" hw="you" pos="PRON">you </w><w c5="VVI" hw="know" pos="VERB">know</w><c c5="PUN">?</c></s></head>

I would like to first check what kind of heading it is (options are type="MAIN", type="SUB" and type="BYLINE") and use <h2>, <h3> or <h4> for these. In addition, I would like to add italics, boltface, etc. to the HTML to account for rend="bo", rend="it", rend="ul" and the like.
I know that I can do the following:

  <xsl:template match='head[@type="MAIN"]'>
    <h3> <xsl:apply-templates/>
</h3>
  </xsl:template>

or:

  <xsl:template match='head[@type="SUB"]'>
    <h4> <xsl:apply-templates/>
</h4>
  </xsl:template>

But how can I avoid having to write something like this for all possible combinations of "rend" and "type"?

Again, sorry for asking such a question - but a knowledgeable reply (or a pointer to a description somewhere on the web) would help me a lot.
Thanks!
Sebastian




 
Old March 19th, 2007, 04:20 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not sure whether you want to add italics, bold etc by choosing a CSS class/style or by adding elements such as <b>, <i>, etc. The solution is probably a bit different for the two cases.

One solution to this is to apply templates to the same element repeatedly in different modes:

  <xsl:template match='head[@type="MAIN"]'>
    <h3>
     <xsl:apply-templates select="." mode="rend"/>
    </h3>
  </xsl:template>

  .. other values of @type similarly ..

  <xsl:template match="head[@rend='bo']" mode="rend">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>

  .. other values of @rend similarly

Alternatively you can apply templates to the attribute node itself:

  <xsl:template match='head[@type="MAIN"]'>
    <h3>
     <xsl:apply-templates select="@rend"/>
    </h3>
  </xsl:template>

  .. other values of @type similarly ..

  <xsl:template match="@rend[.='bo']">
    <b>
      <xsl:apply-templates select="../child::node()"/>
    </b>
  </xsl:template>

  .. other values of @rend similarly


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 20th, 2007, 02:47 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot!! This is exactly what I needed to get going!
Sebastian






Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i handle this thru CSS nivarp CSS Cascading Style Sheets 9 January 17th, 2008 03:03 AM
How to Handle SessionTimeout prankur ASP.NET 1.0 and 1.1 Professional 2 January 16th, 2006 01:45 PM
How to handle...? chawnu Pro PHP 1 December 8th, 2005 05:14 AM
Getting handle of the file in use baburman .NET Framework 2.0 0 October 16th, 2005 10:56 PM
Getting value contained by HANDLE panja_swarup C++ Programming 1 December 16th, 2003 10:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.