Jump to content

Displaying complex elements


walkingbeard

Recommended Posts

Hello!I've got the following <paragraph> element:

<paragraph>  Blah, blah, text.  To go away, click   <link>    <url>http://theurl.com</url>    <new_window>false</new_window>    <text>here</text>  </link>.</paragraph>

I want to be able to transform that into this:

<p>Blah, blah, text.  Click <a href='http://theurl.com>here</a>.

What I've got so far is this:

<xsl:template match='paragraph'>	<div class='paragraphBody'>  <xsl:apply-templates select='link' />  <xsl:value-of select='.' />	</div></xsl:template><xsl:template match='link'>	<a>  <xsl:attribute name='href'> 	 <xsl:value-of select='url' />  </xsl:attribute>    <xsl:if test='new_window=true'> 	 <xsl:attribute name='onClick'>window.open(this.href); return false</xsl:attribute>  </xsl:if>  <xsl:value-of select='text' />	</a></xsl:template>

The trouble is, this outputs first the correct link and then the whole paragraph, including the text held inside the <link> element:

hereBlah, blah, text.  Click http://theurl.com false here.
How can I work this out?
Link to comment
Share on other sites

There are a couple of ways to make links, here you have a few of them:XSL-T

	<xsl:template match="link1">  <span class="link">  <a href="url.xml" target="_self" >text</a>  </span>	</xsl:template>

XML

<link1 />

This link has a set href and text. Now it thows "text".XSL-T

	<xsl:template match="link1">  <span class="link">  	<a href="../XML/contact.xml" target="_self" >    <xsl:value-of select="."/>  	</a>  </span>	</xsl:template>

XML

<link1>One</link1>

This Link has a set href but a variable text. If you change the text in XML its going te be displayed like this. Now it shows "One"XSL-T

	<xsl:template match="link" >	  <span class="link">  	<xsl:variable name="src" select="@src" />  	<xsl:variable name="target" select="@target" />	  	<a href="../XML/{$src}.xml" target="{$target}">    <xsl:value-of select="."/>  	</a>  </span>	</xsl:template>

XML

<link src="61" target="mainFrame">BLA</link>

Everything as variable. Everything is loaded from your XML file. This shows "BLA".XLS-T

<xsl:variable name="image-dir">/images</xsl:variable><xsl:template match="photograph"><img src="{$image-dir}/{href}" target=""/></xsl:template>

XML

<link>  <href>headquarters.htm</href>  <target>mainFrame</target></link>

And you can expand this with text, like you did before.Hopefully you find some usefull ways between the code. :)

Link to comment
Share on other sites

Oops forgot to awnser the whole question.

	<xsl:template match="paragraph">  <p> 	 <xsl:apply-templates/>  </p>		</xsl:template>

This is my code, you can always update it on your own way. The missing part was: <xsl:apply-templates/>.Becouse you say apply paragraph. A part of this paragraph is a link. So it is shown. Later you say make a link. So you get an extra link. But when you say <xsl:apply-templates/>, you mean:first <p> the look further to other expressions and apply them first. When youre done with that apply </p>.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...