Very Interesting XSL Question

F

Fran Cotton

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
 
P

Patrick Peccatte

Fran Cotton said:
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>
 
A

Andy Fish

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.
 
E

Erhard Schwenk

Fran said:
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>
<xsl:eek:therwise>
<xsl:apply-templates />
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:template>

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top