Newbie question on XSL

Discussion in 'XML' started by tanlh_listing@hotmail.com, Dec 22, 2004.

  1. Guest

    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
     
    , Dec 22, 2004
    #1
    1. Advertising

  2. Joris Gillis Guest

    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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Quot capita, tot sententiae"
     
    Joris Gillis, Dec 22, 2004
    #2
    1. Advertising

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

    [tanlh_listing]
    >> 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 ?

    ....
    >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.
    --
    Morris M. Keesan --
     
    Morris M. Keesan, Dec 22, 2004
    #3
  4. On Wed, 22 Dec 2004 10:10:39 GMT, "Joris Gillis" <>
    wrote:

    ><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).

    --
    Morris M. Keesan --
     
    Morris M. Keesan, Dec 22, 2004
    #4
  5. Joris Gillis Guest

    > 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.

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Scio me nihil scire" - Socrates
     
    Joris Gillis, Dec 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Flood
    Replies:
    0
    Views:
    1,024
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,742
    Kevin Flood
    Sep 13, 2004
  3. Vijay singh
    Replies:
    1
    Views:
    436
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  5. Rob Smegma
    Replies:
    1
    Views:
    1,818
    shaun
    Sep 26, 2005
Loading...

Share This Page