Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Very Interesting XSL Question

Reply
Thread Tools

Very Interesting XSL Question

 
 
Fran Cotton
Guest
Posts: n/a
 
      08-05-2003
Hi, I'd greatly appreciate it if someone could cast light on my problem - I
can't seem to find any reference to it anywhere. Consider the following XML:

<paragraph> I am <emphasize>emphasized</emphasize> text inside a paragraph
</paragraph>

How do I go about transforming this into HTML like the following:

<P>I am <EM>emphasized</EM> text inside a paragraph</P>

The problem is that <emphasize> is a child of <paragraph>, but <paragraph>
itself contains textual data too. The best I can do is to get the emphasized
text output after the paragraph, not inside it!

One solution which I do not want to go down because of its unweildyness is:

<paragraph>
<text>I am </text>
<emphasize>emphasized</emphasize>
<text> text inside a paragraph </text>
</paragraph>

Many thanks in advance,

Fran


 
Reply With Quote
 
 
 
 
Patrick Peccatte
Guest
Posts: n/a
 
      08-05-2003
"Fran Cotton" <a@b> a écrit dans le message de news:(E-Mail Removed)...
> Hi, I'd greatly appreciate it if someone could cast light on my problem - I
> can't seem to find any reference to it anywhere. Consider the following XML:
>
> <paragraph> I am <emphasize>emphasized</emphasize> text inside a paragraph
> </paragraph>
>
> How do I go about transforming this into HTML like the following:
>
> <P>I am <EM>emphasized</EM> text inside a paragraph</P>


<xsl:template match="paragraph" >
<P><xsl:apply-templates /></P>
</xsl:template>
<xsl:template match="emphasize" >
<EM><xsl:apply-templates /></EM>
</xsl:template>
--
Patrick Peccatte
www.softexperience.com


 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      08-05-2003
I can't figure out why you think it will cause a problem. If you just want
to switch <paragraph> to <P> and <emphasize> to <EM>, you write:

<xsl:template match="paragraph">
<P><xsl:apply-templates/></P>
</xsl:template>

<xsl:template match="emphasize">
<EM><xsl:apply-templates/></EM>
</xsl:template>

the default template processing takes care of all the text nodes.


"Fran Cotton" <a@b> wrote in message
news:(E-Mail Removed)...
> Hi, I'd greatly appreciate it if someone could cast light on my problem -

I
> can't seem to find any reference to it anywhere. Consider the following

XML:
>
> <paragraph> I am <emphasize>emphasized</emphasize> text inside a paragraph
> </paragraph>
>
> How do I go about transforming this into HTML like the following:
>
> <P>I am <EM>emphasized</EM> text inside a paragraph</P>
>
> The problem is that <emphasize> is a child of <paragraph>, but <paragraph>
> itself contains textual data too. The best I can do is to get the

emphasized
> text output after the paragraph, not inside it!
>
> One solution which I do not want to go down because of its unweildyness

is:
>
> <paragraph>
> <text>I am </text>
> <emphasize>emphasized</emphasize>
> <text> text inside a paragraph </text>
> </paragraph>
>
> Many thanks in advance,
>
> Fran
>
>



 
Reply With Quote
 
Erhard Schwenk
Guest
Posts: n/a
 
      08-05-2003
Fran Cotton wrote:
> Hi, I'd greatly appreciate it if someone could cast light on my problem - I
> can't seem to find any reference to it anywhere. Consider the following XML:
>
> <paragraph> I am <emphasize>emphasized</emphasize> text inside a paragraph
> </paragraph>
>
> How do I go about transforming this into HTML like the following:
>
> <P>I am <EM>emphasized</EM> text inside a paragraph</P>
>
> The problem is that <emphasize> is a child of <paragraph>, but <paragraph>
> itself contains textual data too. The best I can do is to get the emphasized
> text output after the paragraph, not inside it!


Two Solutions.

First one: use the generix <xsl:apply-templates/>. Like this:

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

<xsl:template match="paragraph">
<P>
<xsl:apply-templates/>
</P>
</xsl:template>

<xsl:template match="emphasize">
<em>
<xsl:apply-templates/>
</em>
</xsl:template>

This is the simpler Way.

If you need to selectively process the content of paragraph, you may
want to use node():

<xsl:template match="paragraph">
<p>
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="name()='EM'">
<em><xsl:apply-templates/></em>
</xsl:when>
<xsltherwise>
<xsl:apply-templates />
</xsltherwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>

As you can see, this example is a little bit bad for the second variant.

--
Erhard Schwenk

Akkordeonjugend Baden-Württemberg - http://www.akkordeonjugend.de
K-ITX Webhosting - http://webhosting.k-itx.net

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments