Problem with XSL tranformation: nested elements

M

Max

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
 
M

Martin Honnen

Max said:
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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top