Re: [xsl] mixed content nodes question

Subject: Re: [xsl] mixed content nodes question
From: António Mota <amsmota@xxxxxxxxx>
Date: Fri, 7 Jan 2005 19:17:07 +0000
Or to one who doesn't like if's

        <xsl:template match="dictionary[lookup = '3']>Foo<xsl:/template>
        <xsl:template match="dictionary[lookup = '4']>Foo<xsl:/template>

You have to change your mind from the "for...loop" paradigm to the
template paradigm, that's not easy at first but when you do all things
came in place...


On Fri, 7 Jan 2005 11:10:31 -0800, Mark Lundquist <ml@xxxxxxxxxxxxxx> wrote:
> 
> Hi Jeb,
> 
> > From: Jeb Boniakowski [mailto:jeb@xxxxxxxxxxx]
> >
> > <...snip...>
> > I'd imagine I'd have some part of my xsl sheet include a long 'when'
> > block and switch on the lookup attribute's values so i'd have:
> > <xsl:when test="3">Foo</xsl:when>
> > <xsl:when test="4">Bar</xsl:when>
> >
> > But I can't figure out how to do the actual transforming of only those
> > <dictionary> tags in place, leaving the rest of the file unchanged.
> > I've tried apply-templates in the a template matching "/", then having
> > a template matching "*" do a test to see if the current node is
> > <dictionary>, if so, transform and write out, otherwise, write out as
> > it is, but I can't get it to work right.
> >
> > If it's vastly easier to process if instead of having:
> > <dictionary lookup="3"/> -> Foo
> > I have:
> > <dictionary>3</dictionary> -> Foo
> >
> > I might be able to make that change to the xml format, but I'm still
> > confused about how to deal with mixed nodes.  I feel like people must
> > do this all the time, if they have bits of HTML embedded in larger XML
> > trees they must have markup tags like <b>, <a> that make the nodes
> > mixed content
> 
> Indeed... all the time.
> 
> First of all, you need a template like this:
> 
>         <xsl:template match="node() | @*">
>           <xsl:copy>
>                 <xsl:apply-templates select="node() | @*" />
>           </xsl:copy>
>         </xsl:template>
> 
> Later, read http://www.dpawson.co.uk/xsl/sect2/defaultrule.html to learn
> what you are fixing with the above template :-).
> 
> I put that template in its own stylesheet and then do something like
> 
>         <xsl:import href="xlst/common/identity.xslt" />
> 
> from all my other stylesheets that need this.  But for now, you can just
> write the template inline.
> 
> Then, add a template like this:
> 
>         <xsl:template match="dictionary">
>            <xsl:if test="@lookup = '3'">Foo</xsl:if>
>            <xsl:if test="@lookup = '4'">Bar</xsl:if>
>         <xsl:/template>
> 
> The above template uses the "@" shorthand notation for the attribute:: axis,
> which you can read up on later.
> 
> HTH,
> Mark

Current Thread