<a href> and xsl

L

Larry

Hi,

I'm trying to get the following working but I'm at a loss and dont
know what is wrong with it:

<div class="name">
<a href=" <xsl:apply-templates select="key[text()='Location']"
mode="getValue" /> " target="_blank">
<xsl:apply-templates select="key[text()='Name']" mode="getValue" />
<xsl:text> </xsl:text>
</a>
</div>

any help?

thansks
 
B

Bjoern Hoehrmann

* Larry wrote in comp.text.xml:
Hi,

I'm trying to get the following working but I'm at a loss and dont
know what is wrong with it:

<div class="name">
<a href=" <xsl:apply-templates select="key[text()='Location']"
mode="getValue" /> " target="_blank">
<xsl:apply-templates select="key[text()='Name']" mode="getValue" />
<xsl:text> </xsl:text>
</a>
</div>

You cannot use elements in attribute values, you have to use the special
xsl:attribute element or attribute value templates, refer to the XSLT
1.0 specification for details:

http://www.w3.org/TR/xslt#creating-attributes
http://www.w3.org/TR/xslt#dt-attribute-value-template

Only the former will allow you to use apply-templates.
 
J

Joseph Kesselman

<a href=" <xsl:apply-templates select="key[text()='Location']"
> mode="getValue" /> " target="_blank">

You can't issue an apply-templates call from inside an attribute value. Try:

<a target="_blank">
<xsl:attribute name="href">
<xsl:apply-templates select="key[text()='Location']"
mode="getValue" /> "

.... in other words, create the <a> element, then add an attribute that
has a computed value.
 
L

Larry

Joseph Kesselman said:
<a target="_blank">
<xsl:attribute name="href">
<xsl:apply-templates select="key[text()='Location']"
mode="getValue" /> "

... in other words, create the <a> element, then add an attribute that
has a computed value.

ok, I got it. But now I'm having some trouble making a substitution,
here's some code:

<xsl:template match="dict">
<div>
<div class="name">
<a target="_blank">
<xsl:attribute name="href">
<xsl:apply-templates select="key[text()='Location']"
mode="getAsUri"/>
</xsl:attribute>
<xsl:apply-templates select="key[text()='Name']" mode="getValue" />
<xsl:text> </xsl:text>
</a>
</div>
</div>
</xsl:template>

<xsl:template match="key" mode="getValue">
<xsl:value-of select="following-sibling::*[1]" />
</xsl:template>

<xsl:template match="key" mode="getAsUri">
<xsl:variable name="txt">
<xsl:apply-templates select="." mode="getValue" />
</xsl:variable>
<xsl:text>http://127.0.0.1:8000/</xsl:text>
<xsl:value-of select="substring-after($txt,'file:///')"
disable-output-escaping="yes" />
</xsl:template>

It should turn:

file:///Users/etc...

to:

http://127.0.0.1:8000/Users/etc...

sadly, it doesn't work properly as I only get http://127.0.0.1:8000/
without the /Users/etc... part

I can't work out what is wrong with the code

any help?

thanks
 
P

Peter Flynn

Larry said:
Joseph Kesselman said:
<a target="_blank">
<xsl:attribute name="href">
<xsl:apply-templates select="key[text()='Location']"
mode="getValue" /> "

... in other words, create the <a> element, then add an attribute that
has a computed value.

ok, I got it. But now I'm having some trouble making a substitution,
here's some code:

<xsl:template match="dict">
<div>
<div class="name">
<a target="_blank">
<xsl:attribute name="href">
<xsl:apply-templates select="key[text()='Location']" mode="getAsUri"/>

That will select all child elements of dict called key, whose (implicit)
first stretch of unmarked character data content is equal to "Location".
The template for key will then provide a value starting with the quoted
http method, IP address and port, plus the delocalised value of the text
content of the first element following the key, omitting to escape the
ampersands. Is this what you meant? Presumably the element following
such a key element does indeed contain a URI.
</xsl:attribute>
<xsl:apply-templates select="key[text()='Name']" mode="getValue" />
<xsl:text> </xsl:text>
</a>
</div>
</div>
</xsl:template>

<xsl:template match="key" mode="getValue">
<xsl:value-of select="following-sibling::*[1]" />
</xsl:template>

<xsl:template match="key" mode="getAsUri">
<xsl:variable name="txt">
<xsl:apply-templates select="." mode="getValue" />
</xsl:variable>
<xsl:text>http://127.0.0.1:8000/</xsl:text>
<xsl:value-of select="substring-after($txt,'file:///')"
disable-output-escaping="yes" />
</xsl:template>

It should turn:

file:///Users/etc...

to:

http://127.0.0.1:8000/Users/etc...

sadly, it doesn't work properly as I only get http://127.0.0.1:8000/
without the /Users/etc... part

I can't work out what is wrong with the code

If you provided us with some sample data so that we didn't have to work
blind it would help...

///Peter
 
J

Joe Kesselman

help!! I just want to transform this:
<tag>file:///Users/</tag>
> to this:
<tag>http://127.0.0.1:8000/Users</tag>

If you want the URI to be relative to where the document was loaded
from, why not just use a relative URI in the first place:

<tag>/Users</tag>

That makes this the browser's problem... and any browser ought to handle
it properly.
 
P

Peter Flynn

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top