Newbie question on XSL

T

tanlh_listing

I have the following XML :
<VendorInfo groupName="AA">
<VendorAddress>ABC Building</VendorAddress>
<VendorAddress>Street 77</VendorAddress>
<VendorAddress>LA</VendorAddress>
<VendorAddress>USA</VendorAddress>
<VendorAddress>1234-1234</VendorAddress>
</VendorInfo>

Here is me XSL :
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML><BODY>
Vender Address :
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
</BODY></HTML>
</xsl:template>
</xsl:stylesheet>

Output = 5 lines of "ABC Building"
How do I get it to print the completed address ?

Thanks,
LH
 
J

Joris Gillis

Hi,
I have the following XML :
<xsl:template match="/">
<HTML><BODY>
Vender Address :
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
<xsl:value-of select="VendorInfo/VendorAddress"/>
</BODY></HTML>
</xsl:template>
</xsl:stylesheet>

Output = 5 lines of "ABC Building"
How do I get it to print the completed address ?

The Xpath expression "VendorInfo/VendorAddress" selects each and every (in this case 5) VendorAddress elements in your XML.
The 'xsl:value-of' typically processes _only the first_ element that was returned with its 'select' attribute. Just repeating the same 'xsl:value-of' will logically result in 5 times the same output.

Theretically, your problem is solved with this:
<xsl:value-of select="VendorInfo/VendorAddress[1]"/>
<xsl:value-of select="VendorInfo/VendorAddress[2]"/>
<xsl:value-of select="VendorInfo/VendorAddress[3]"/>
<xsl:value-of select="VendorInfo/VendorAddress[4]"/>
<xsl:value-of select="VendorInfo/VendorAddress[5]"/>

But don't use that.
In stead use an 'xsl:apply-templates' element; it will process _all_ nodes that the 'select' attribute returns.
e.g:
<xsl:apply-templates select="VendorInfo/VendorAddress"/>

most likely, you'll want a seperator off some kind or a containing element around the returned values. You can do that with another template.

On the whole, you can use something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="html" indent="yes"/>

<xsl:template match="/">
<html><body>
Vender Address :
<xsl:apply-templates select="VendorInfo/VendorAddress"/>
</body></html>
</xsl:template>

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

</xsl:stylesheet>

That will result in:
<html><body>
Vender Address :
<p>ABC Building</p><p>Street 77</p><p>LA</p><p>USA</p><p>1234-1234</p></body></html>


regards,
 
M

Morris M. Keesan

On Wed, 22 Dec 2004 10:10:39 GMT, "Joris Gillis" wrote:

[tanlh_listing]
....
In stead use an 'xsl:apply-templates' element; it will process _all_ nodes that the 'select' attribute returns.

Alternatively, use an "xsl:for-each" element:

Vendor Address :
<xsl:for-each select="VendorInfo/VendorAddress">
<xsl:value-of select="."/>
</xsl:for-each>

On an unrelated note, you should get into the habit of writing your HTML
tags in lower-cased, e.g. <html><body> instead of <HTML><BODY>, to make
the transition to XHTML easier.
 
M

Morris M. Keesan

<xsl:template match="/">
<html><body>
Vender Address :
<xsl:apply-templates select="VendorInfo/VendorAddress"/>
</body></html>
</xsl:template>

Better, in my opinion, would be

<xsl:template match="/">
<html><body>
<xsl:apply-templates select="VendorInfo"/>
</body></html>
</xsl:template>

<xsl:template match="VendorInfo">
Vendor Address:
<xsl:apply-templates select="VendorAddress"/>
</xsl:template>

Just in case the number of VendorInfo elements is something other than
one (we haven't seen a DTD or Schema to know whether this is possible).
 
J

Joris Gillis

Better, in my opinion, would be
<xsl:template match="/">
<html><body>
<xsl:apply-templates select="VendorInfo"/>
</body></html>
</xsl:template>

<xsl:template match="VendorInfo">
Vendor Address:
<xsl:apply-templates select="VendorAddress"/>
</xsl:template>

Just in case the number of VendorInfo elements is something other than
one (we haven't seen a DTD or Schema to know whether this is possible).
Yes , I agree with you.
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top