Using value-of within double quotes??

B

bissatch

Hi,

I have a an XML file:

<xml...
<store>
<book>
<title>Darkness at Noon</title>
<price>12.99</price>
<url>http://www.amazon.com/...</url>
</book>
.
.
.
</store>

I am trying to outout it using the following segment in my XSL file:

<xsl:for-each select="store/book">
<p><strong><value-of select="title" /></strong></p>
<p>&pound;<value-of select="price" /></p>
<p><a href="<value-of select="url" />">Buy book</a></p>
</xsl:for-each>

Now I was certain that this would fail as it is not valid XML but I
have no idea how I would add a value to a HTML attribute (within
double quotes). Obviously Im going about this the wrong way but could
someone please tell me the correct method for this. Thanks

Burnsy
 
B

bissatch

Figured it out (eventually)

<xsl:for-each select="store/book">
<p><strong><value-of select="title" /></strong></p>
<p>&pound;<value-of select="price" /></p>
<p><a href="{url}">Buy book</a></p>
</xsl:for-each>


or use xsl:attribute but havent yet tried that one.
 
J

Joe Kesselman

<p><a href="{url}">Buy book</a></p>

Yep; that's known as an Attribute Value Template (AVT), and is the
simplest way to generate an attribute with a known name.
or use xsl:attribute but havent yet tried that one.

More often used when you need to programmatically generate the
attribute's name, or when you need fancier logic in setting its value
than an AVT can support:

<p><a><xsl:attribute name="href" select="url"/>Buy book</a></p>
or

<p><a><xsl:attribute name="href"><xsl:value-of
select="url"/></xsl:attribute>Buy book</a></p>

or, for stylesheet readability reasons:
<p><a>
<xsl:attribute name="href"/>
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:text>Buy book</xsl:text>
</a></p>

.... or any of the equivalents thereof. (XSLT being a programming
language, there is usually more than one way to achieve a given result.)
 
M

Martin Honnen

Joe said:
More often used when you need to programmatically generate the
attribute's name, or when you need fancier logic in setting its value
than an AVT can support:

<p><a><xsl:attribute name="href" select="url"/>Buy book</a></p>

But xsl:attribute in XSLT 1.0 does not have a select attribute
<http://www.w3.org/TR/xslt#creating-attributes>
it is a bit dangerous to show that example above without mentioning it
is only supported in XSLT 2.0
<http://www.w3.org/TR/xslt20/#creating-attributes>
 
J

Joe Kesselman

Martin said:
But xsl:attribute in XSLT 1.0 does not have a select attribute

Whups. You're right, very sloppy of me. I did illustrate the
value-as-content solution as well; that does work in 1.0.
 

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
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top