Very Interesting XSL Question

Discussion in 'XML' started by Fran Cotton, Aug 5, 2003.

  1. Fran Cotton

    Fran Cotton Guest

    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
     
    Fran Cotton, Aug 5, 2003
    #1
    1. Advertising

  2. "Fran Cotton" <a@b> a écrit dans le message de news:...
    > 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
     
    Patrick Peccatte, Aug 5, 2003
    #2
    1. Advertising

  3. Fran Cotton

    Andy Fish Guest

    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:...
    > 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
    >
    >
     
    Andy Fish, Aug 5, 2003
    #3
  4. 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>
    <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.

    --
    Erhard Schwenk

    Akkordeonjugend Baden-Württemberg - http://www.akkordeonjugend.de
    K-ITX Webhosting - http://webhosting.k-itx.net
     
    Erhard Schwenk, Aug 5, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    483
    Raymond Hettinger
    Jul 27, 2003
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,631
    Merrill & Michele
    Oct 19, 2004
  4. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    427
    jacob navia
    Oct 17, 2004
  5. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    519
    Dave Thompson
    Feb 14, 2005
Loading...

Share This Page