RE: [xsl] need help with converting attribute into elements

Subject: RE: [xsl] need help with converting attribute into elements
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Tue, 14 Oct 2003 18:13:16 +0100
The following solution (using a few 2.0 features) is perhaps a little
more elegant.

<xsl:template match="text">
<xsl:apply-templates select="." mode="inline"/>
</xsl:template>

<xsl:template match="*[@bold='true']" mode="inline" priority="10">
<b><xsl:sequence select="my:inline(., @bold)"/></b>
</xsl:template>

<xsl:template match="*[@italic='true']" mode="inline" priority="9">
<i><xsl:sequence select="my:inline(., @italic)"/></i>
</xsl:template>

<xsl:template match="*[@underline='true']" mode="inline" priority="8">
<u><xsl:sequence select="my:inline(., @underline)"/></u>
</xsl:template>

<xsl:template match="*" mode="inline" priority="7">
<xsl:apply-templates/>
</xsl:template>

<xsl:function name="my:inline" as="element()">
  <xsl:param name="elem" as="element()"/>
  <xsl:param name="att" as="attribute()"/>
  <xsl:variable name="x">
    <xsl:copy>
    <xsl:copy-of select="$elem/(node() | @*) except $att"/>
    </xsl:copy>
  </xsl:variable>
  <xsl:apply-templates select="$x" mode="inline"/>
</xsl:function>

Michael Kay
  

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Aleš Peregrin
> Sent: 14 October 2003 12:12
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] need help with converting attribute into elements
> 
> 
> Hi,
> thanx for your time. Your solution is now clear for me.
> Now I know I was right: there is no easy way. I gues there 
> are some things XSLT can't do. So thanx again.
> 
> Ales Peregrin
> 
> ----- Original Message -----
> From: "Rod Humphris - FLPTN" <rod.humphris@xxxxxxxxxxxxxx>
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Tuesday, October 14, 2003 12:19 PM
> X-Scanned: By Symantec Anti-Virus Scan Engine
> Subject: Re: [xsl] need help with converting attribute into elements
> 
> 
> > Code nested choose/when/otherwise blocks to cover all the 
> combinations
> that
> > you want to catch. It isn't pretty but it will work. 
> Copy/paste will 
> > get
> you
> > there fast enough!
> >
> > <xsl:template match="text">
> >  <p>
> >   <xsl:choose>
> >    <xsl:when test="@bold='true'">
> >     <b>
> >      <xsl:choose>
> >       <xsl:when test="@italic='true'">
> >        <i>
> >         <xsl:choose>
> >           <xsl:when test="@underline='true'">
> >            <u>
> >             <xsl:value-of select="."/>
> >           </u>
> >          <xsl:otherwise>
> >           <xsl:value-of select="."/>
> >         </xsl:otherwise>
> >        </xsl:choose>
> >        </i>
> >        </xsl:when>
> >        <xsl:otherwise>
> >         etc, etc....
> >     </b>
> >    </xsl:when>
> >    <xsl:otherwise>
> >
> >    </xsl:otherwise>
> >   </xsl:choose>
> >  </p>
> > </xsl:template>
> > There you are I've basically done it for you, just fill in 
> the missing
> bits.
> >
> > I can't myself think of a better way. I think perhaps that 
> sometimes 
> > xslt just is verbose and this may be one of them.
> >
> > The best I can suggest is that you could write the code 
> inside a named 
> > template (look it up if you don't know) which takes an element as a 
> > parameter and then at least you won't have to write it 
> again and you 
> > can
> use
> > it on other elements besides a 'text' one. You could then
> xsl:include/import
> > it from a library xslt to make it more reusable again.
> >
> > Ah just saw Jarno's. Much better. Take your pick.
> >
> > Cheers
> >
> > Rod
> >
> > -----Original Message-----
> > From: Aleš Peregrin [mailto:ales.peregrin@xxxxxxxxxx]
> > Sent: 14 October 2003 10:49
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > X-Scanned: By Symantec Anti-Virus Scan Engine
> > Subject: RE: [xsl] need help with converting attribute into elements
> >
> >
> > Hi Rod,
> >
> > thanx for your solution suggestion.
> > I'm not sure, wheter I understand it right. Does it mean to 
> write it 
> > eight times, once for each combinatoion of the atribute 
> values, e.g. 
> > first for "all true", than <otherwise> one of them false 
> and so on? Or 
> > is there some easier way? Could you please write some more detailed 
> > example?
> >
> > thanx
> >
> > Ales Peregrin
> >
> > ----- Original Message -----
> > From: "Rod Humphris - FLPTN" <rod.humphris@xxxxxxxxxxxxxx>
> > To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> > Sent: Tuesday, October 14, 2003 11:05 AM
> > X-Scanned: By Symantec Anti-Virus Scan Engine
> > X-Scanned: By Symantec Anti-Virus Scan Engine
> > Subject: RE: [xsl] need help with converting attribute into elements
> >
> >
> > > Hi Ales
> > >
> > > I expect someone could suggest a prettier and more 
> modular approach 
> > > but nested choose blocks would do it:
> > >
> > > <xsl:template match="text">
> > >  <p>
> > >   <xsl:choose>
> > >    <xsl:when test="@bold='true'">
> > >     <b>
> > >      <xsl:choose>
> > >       <xsl:when test="@italic='true'">
> > >        <i>
> > >          etc, etc.....
> > >     </b>
> > >    </xsl:when>
> > >    <xsl:otherwise>
> > >
> > >    </xsl:otherwise>
> > >   </xsl:choose>
> > >  </p>
> > > </xsl:template>
> > >
> > > Cheers
> > >
> > > Rod
> > >
> > >
> > > -----Original Message-----
> > > From: Aleš Peregrin [mailto:ales.peregrin@xxxxxxxxxx]
> > > Sent: 14 October 2003 09:47
> > > To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> > > X-Scanned: By Symantec Anti-Virus Scan Engine
> > > Subject: RE: [xsl] need help with converting attribute 
> into elements
> > >
> > >
> > > Hi!
> > > I'm trying to make a XSLT stylexheet for transforming XML 
> document 
> > > into
> > HTML
> > > and there is a problem I'm unable to solve, even after a week of 
> > > trying
> > and
> > > searching all over the internet. The problem looks quite 
> easy to me 
> > > and
> it
> > > seems I'm missing something important about XSL  
> language. Here it 
> > > goes:
> > >
> > > I have an XML tag which looks like this: <text bold=true 
> italic=true 
> > > underline=false>Help!</text> In this case, the result in 
> HTML should 
> > > be: <font>
> > >    <b>
> > >       <i>
> > >          Help!
> > >       </i>
> > >    </b>
> > > </font>
> > >
> > > I simply need to transform the attributes into HTML 
> elements so that 
> > > any
> > of
> > > the possible combinations of the atributes' values would be 
> > > transformed correctly.
> > >
> > > Can anyone help me?
> > > Thanx.
> > > Ales Peregrin
> > >
> > >
> > >  XSL-List info and archive:  
> > > http://www.mulberrytech.com/xsl/xsl-list
> > >
> > >
> > > 
> ____________________________________________________________________
> > > ____
> > > This email has been scanned for all viruses by the 
> MessageLabs Email
> > > Security System. For more information on a proactive 
> email security
> > > service working around the clock, around the globe, visit
> > > http://www.messagelabs.com
> > > 
> ______________________________________________________________
> __________
> > >
> > >
> > > ________________________________________________________________
> > > Any opinions expressed in this email are those of the 
> individual and 
> > > not
> > necessarily the Company. Unless expressly stated to the 
> contrary, this
> email
> > is not intended to give rise to a new, or affect an existing, 
> > contractual
> or
> > other legal relationship.This email and any files 
> transmitted with it, 
> > including replies and forwarded copies which may contain 
> alterations) 
> > subsequently transmitted from the Company, are confidential 
> and solely 
> > for the use of the intended recipient. The unauthorised use, 
> > disclosure or copying of this email, or any other information 
> > contained or attached,is prohibited and could, in certain 
> > circumstances, be a criminal offence.
> > >
> > > If you have received this email in error please notify 
> the sender as
> soon
> > as possible.
> > >
> > > This footnote also confirms that this email message has 
> been swept 
> > > for
> the
> > presence of computer viruses.
> > >
> > > www.focusdiy.co.uk 
> > > _________________________________________________________________
> > >
> > > 
> ____________________________________________________________________
> > > ____
> > > This email has been scanned for all viruses by the 
> MessageLabs Email
> > > Security System. For more information on a proactive 
> email security
> > > service working around the clock, around the globe, visit
> > > http://www.messagelabs.com
> > > 
> ______________________________________________________________
> __________
> > >
> > >  XSL-List info and archive:  
> > > http://www.mulberrytech.com/xsl/xsl-list
> > >
> > >
> >
> >
> >
> >  XSL-List info and archive:  
> http://www.mulberrytech.com/xsl/xsl-list
> >
> >
> > 
> ______________________________________________________________________
> > __
> > This email has been scanned for all viruses by the MessageLabs Email
> > Security System. For more information on a proactive email security
> > service working around the clock, around the globe, visit
> > http://www.messagelabs.com
> > 
> ______________________________________________________________
> __________
> >
> > 
> ______________________________________________________________________
> > __
> > This email has been scanned for all viruses by the MessageLabs Email
> > Security System. For more information on a proactive email security
> > service working around the clock, around the globe, visit
> > http://www.messagelabs.com
> > 
> ______________________________________________________________
> __________
> >
> >  XSL-List info and archive:  
> http://www.mulberrytech.com/xsl/xsl-list
> >
> >
> 
> 
>  XSL-List info 
> and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread