Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 19th, 2007, 03:17 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
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




Reply With Quote
  #2 (permalink)  
Old March 19th, 2007, 04:20 PM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #3 (permalink)  
Old March 20th, 2007, 02:47 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.