[xsl] Re: matching multiple times, outputting once?

Subject: [xsl] Re: matching multiple times, outputting once?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 6 Nov 2001 03:54:57 -0800 (PST)
OK, I see that I misunderstood the problem and provided the solution to the reverse
problem...

Here's the solution the other way around. This essentially uses Mike's idea, but
concentrates all processing just in one moded template.

xml source document:
-------------------
<contents>
    <emphasis bold="Yes">Hello</emphasis>
    <emphasis bold="Yes" italic="Yes">Hello</emphasis>
    <emphasis bold="Yes" italic="Yes" underline="Yes">Hello</emphasis>
</contents>


Stylesheet:
----------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="emphasis">
      <xsl:apply-templates select="@*[1]" mode="emphasis">
        <xsl:with-param name="children" select="child::node()"/>
        <xsl:with-param name="other-attributes" select="@*[position()!=1]"/>
      </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="(@bold | @italic | @underline)[.='Yes']" mode="emphasis">
      <xsl:param name="children"/>
      <xsl:param name="other-attributes"/>
      <xsl:element name="{substring(name(), 1, 1)}">
        <xsl:choose>
        <xsl:when test="$other-attributes">
          <xsl:apply-templates select="$other-attributes[1]" mode="emphasis">
            <xsl:with-param name="children" select="$children"/>
            <xsl:with-param name="other-attributes"
                            select="$other-attributes[position()!=1]"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="$children"/>
        </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Result:
------
<b>Hello</b>
<b>
<i>Hello</i>
</b>
<b>
<i>
<u>Hello</u>
</i>
</b>


Cheers,
Dimitre Novatchev.


--- Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> Here's the solution to your problem:
> 
> xml source document:
> -------------------
> <contents>
> <b>Hello</b>
> <i><b>Hello</b></i>
> <u><i><b>Hello</b></i></u>
> </contents>
> 
> 
> Stylesheet:
> ----------
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:trans="myTrans" exclude-result-prefixes="trans"
> >
> 
>   <xsl:output indent="yes" omit-xml-declaration="yes"/>
> 
>   <trans:trans>
>     <in>b</in>
>     <out>bold</out>
>     <in>i</in>
>     <out>italic</out>
>     <in>u</in>
>     <out>underline</out>
>   </trans:trans>
>   
>   <xsl:variable name="vTrans" select="document('')/*/trans:*"/>
>   
>   <xsl:template match="b | i | u">
>     <emphasis>
>       <xsl:apply-templates select="." mode="emph"/>
>     </emphasis>
>   </xsl:template>
>   
>   <xsl:template match="b | i | u" mode="emph">
>     <xsl:variable name="vEmphName" 
>                   select="$vTrans/in[. = name(current())]
>                                    /following-sibling::out[1]"/>
>       <xsl:attribute name="{$vEmphName}">yes</xsl:attribute> 
>       <xsl:apply-templates mode="emph"/>
>   </xsl:template>
>   
> </xsl:stylesheet>
> 
> 
> Result:
> ------
> <emphasis bold="yes">Hello</emphasis>
> <emphasis italic="yes" bold="yes">Hello</emphasis>
> <emphasis underline="yes" italic="yes" bold="yes">Hello</emphasis>
> 
> 
> Hope this helped.
> 
> Cheers,
> Dimitre Novatchev.
> 
> 
> "McKeever, Marty" <marty dot mckeever at bankofamerica dot com> wrote:
> 
> This is really bugging me, because i thought it would be simple.  Maybe it
> is, and i'm just having a mental block -- thanks for your help.
> 
> Here are 3 possibilities that i have to match for, and 3 desired outputs:
> 
> <emphasis bold="Yes">Hello</emphasis>
> <emphasis bold="Yes" italic="Yes">Hello</emphasis>
> <emphasis bold="Yes" italic="Yes" underline="Yes">Hello</emphasis>
> 
> <b>Hello</b>
> <i><b>Hello</b></i>
> <u><i><b>Hello</b></i></u>
> 
> and every possible combination thereof.
> 
> 
> I have had no luck writing an intelligent template rule for these -- the
> only way i've found so far is a deeply nested <xsl:choose> which takes every
> possible combination into account.  This can't be the best way to do this.
> 
> Everything else i've tried either matches only one rule, or outputs "Hello"
> multiple times, one for each style.
> 
> There has to be an elegant solution i'm missing...
> 
> (and yes, i know i could easily write this out as <span
> style="font-style:bold,italic;text-decoration:underline">Hello</span>, but
> that's not what i'm looking for here.)
> 
> TIA!
> marty
> 
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Find a job, post your resume.
> http://careers.yahoo.com
> 


__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread