Problem with XSL tranformation: nested elements

Discussion in 'XML' started by Max, Aug 19, 2007.

  1. Max

    Max Guest

    Hi!
    I have a problem with an XSL transformation. This is the XML file to
    transform: there's an "article" with one element of type "text". There
    are two nested elements of type "image" and "text":

    <article>

    <element type="text ">
    <![CDATA[Some introductory text]]>

    <element name="photo" type="image" ext="jpg"/>

    <element type="text ">
    <![CDATA[Text related to the image]]>
    </element>

    </element>

    </article>

    My goal is to print:

    Some introductory text
    <img src="photo.jpg" />.
    Text related to the image.

    This is the XSL:

    <xsl:template match="/">

    <xsl:apply-templates select="/article/element[@type = 'text]"/>

    <xsl:apply-templates select="article/element/element[@type =
    'image']"/>

    </xsl:template>

    <xsl:template match="article/element[@type = 'text']">

    <xsl:value-of select="." />

    </xsl:template>

    <xsl:template match="/article/element/element[@type = 'image']">

    <img src="{@name}.{@ext}" />.
    <xsl:apply-templates select="../element[@type = 'text-body']"/>

    </xsl:template>

    The problem is that the XSL prints the first and the second text
    together. This is the result of the XSL:

    Some introductory text. Text related to the image.
    <img src="photo.jpg" />.
    Text related to the image.

    How can I solve the problem? Thanks!

    Bye, Max
     
    Max, Aug 19, 2007
    #1
    1. Advertising

  2. Max wrote:

    > I have a problem with an XSL transformation. This is the XML file to
    > transform: there's an "article" with one element of type "text". There
    > are two nested elements of type "image" and "text":
    >
    > <article>
    >
    > <element type="text ">
    > <![CDATA[Some introductory text]]>
    >
    > <element name="photo" type="image" ext="jpg"/>
    >
    > <element type="text ">
    > <![CDATA[Text related to the image]]>
    > </element>
    >
    > </element>
    >
    > </article>
    >
    > My goal is to print:
    >
    > Some introductory text
    > <img src="photo.jpg" />.
    > Text related to the image.


    You need just one template

    <xsl:template match="element[@type = 'image']">
    <img src="{@name}.{ext}" />
    <xsl:apply-templates />
    </xsl:template>

    for the rest the default templates will kick in and do what you want
    (minus some white space issues).


    > <xsl:template match="article/element[@type = 'text']">
    >
    > <xsl:value-of select="." />
    >
    > </xsl:template>


    The string value of an element is the text contents of all children and
    descendants.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 19, 2007
    #2
    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. Michael
    Replies:
    3
    Views:
    1,264
    Sudsy
    Sep 30, 2004
  2. Replies:
    1
    Views:
    3,637
    A. Bolmarcich
    May 27, 2005
  3. Rob Smegma
    Replies:
    1
    Views:
    1,826
    shaun
    Sep 26, 2005
  4. Tomislav Jakopec

    Tranformation time

    Tomislav Jakopec, Nov 2, 2005, in forum: XML
    Replies:
    1
    Views:
    392
    Nick Kew
    Nov 2, 2005
  5. BansalMaddy
    Replies:
    4
    Views:
    246
    BansalMaddy
    Nov 29, 2010
Loading...

Share This Page