Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Problem with XSL tranformation: nested elements

Reply
Thread Tools

Problem with XSL tranformation: nested elements

 
 
Max
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-19-2007
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/
 
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 to group elements? [xsl newbie] Rob Smegma XML 1 09-26-2005 10:59 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM
XSL Recursive nested elements woe GR33DY XML 0 06-24-2004 09:34 AM
xsl:if and nested td tag problem Steven XML 3 07-23-2003 03:31 PM



Advertisments