Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 February 25th, 2005, 05:24 AM
Authorized User
 
Join Date: Jun 2003
Location: Gent, , Belgium.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Kabe
Default xsl:template match 'overlapping' ?

example 1
------------
xml: <header>hello</header>

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

output: <h2>hello</h2>

example 2
xml: <header remark="1">hello</header>

xsl: I want the value of attribute remark added as value for a class (see output). But the purpose is, where i use 'remark="value"' it is added the the element as 'class="value"' whatever the element is.

output: <h2 class="1">hello</h2>

examples:
<header remark="1">hello</header> -> <h2 class="1">hello</h2>
<para remark="2">test</para> -> <p class="2">test</p>

thus, whatever the element, attribute remark should be added to the element as class="value"

Extra:
suppose have this:
<para>test</para> -> <p class="format1>test</p>
<para remark="2">test</para> -> <p class="format1 2">test</p>

default para is formatted as <p class="format1">, when adding attribute remark, the value of it is added to the class value.

thx,
kb
 
Old February 25th, 2005, 06:03 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The simplest solution is probably to apply templates to the attributes as well as the child elements:

<xsl:template match="header">
  <h2 class="format1"><xsl:apply-templates select="node()|@*"/></h2>
</xsl:template>

<xsl:template match="@remark">
  <xsl:attribute name="class">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

Note that the class defined using xsl:attribute takes precedence over the one in the literal result element.

You can also achieve this using attribute sets:

<xsl:template match="header">
  <h2 use-attribute-sets="class-set">
     <xsl:apply-templates/>
  </h2>
</xsl:template>

<xsl:attribute-set name="class-set">
  <xsl:attribute name="class">
     <xsl:choose>
        <xsl:when test="@remark=1">
            etc...

In XSLT 2.0 you can use functions:

<xsl:template match="header">
  <h2 class="{f:compute-class(.)}">
     <xsl:apply-templates/>
  </h2>
</xsl:template>




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference




Similar Threads
Thread Thread Starter Forum Replies Last Post
Param in template match=" " iceandrews XSLT 2 May 7th, 2008 07:37 AM
value-of inside template match RoeZ XSLT 5 April 17th, 2008 12:09 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
template m,atch doesnt match Tomi XSLT 1 March 9th, 2007 07:56 AM
help with xsl template match enT XSLT 9 September 24th, 2003 06:21 AM





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