DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Greg Rothlander Guest

    Getting the attribute value of an XML Element


    I have not needed to access an elements attribute value since I've started
    working with XML and today I need to figure it out but I'm not having much
    luck. This should be a very easy exercise but I think I'm missing something
    very simple.

    Here is a sample of the stream:

    <TotalSales Company="Universal">
    <DailySales>
    <DailySalesDetail Date="12/01/2001">
    <Units>200</Units>

  2. #2
    Russell Jones Guest

    Re: Getting the attribute value of an XML Element

    If I understood your question, you simply wanted to put the company name in
    the table header. This should work, or at least give you a good idea of how
    to proceed.

    <xsl:template match="TotalSales">
    <table width="100%" border="1">
    <tr><th><xsl:value-of select="@Company"/></th></tr>
    <xsl:for-each select="DailySales/DailySalesDetail">
    <tr><td><value-of select="Units"/></td></tr>
    </xsl:for-each>
    </table>
    </xsl:template>

    Russell Jones
    Sr. Web Development Editor,
    DevX.com

    "Greg Rothlander" <grothlander@austin400.com> wrote in message
    news:3b376bf0$1@news.devx.com...
    >
    > I have not needed to access an elements attribute value since I've started
    > working with XML and today I need to figure it out but I'm not having much
    > luck. This should be a very easy exercise but I think I'm missing

    something
    > very simple.
    >
    > Here is a sample of the stream:
    >
    > <TotalSales Company="Universal">
    > <DailySales>
    > <DailySalesDetail Date="12/01/2001">
    > <Units>200</Units>
    > .
    > .
    > .
    >
    > I'm using XSL to generate a table and I need to include the company name
    > as a header and use the date attribut as part of a row in a table. I've
    > been using the following XSL code but I'm not sure this is the best way to
    > handle what I'm doing. Now that the <Company> element has become an

    attribute
    > of the <TotalSales> element, I'm not sure how to gets it's value out. All
    > of the examples in the numerous books I have do not use the <xsl:template
    > match="/"> like I am using it in my code. The examples I have match a

    particular
    > element name like <... match="DailySales" >. I've not had much luck with
    > this method so I've been coding it as follows...
    >
    > <?xml version="1.0" ?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    > <xsl:template match="/">
    >
    > <xsl:for-each select="TotalSales/DailySales/DailySalesDetail">
    > <table width="100%" border="1">
    > <tr><td><value-of select="Units"/></td></tr>
    > </table>
    > </xsl:for-each>
    > .
    > .
    > .
    >
    >
    > How do I access the "Company" and "Date" elements in the example stream?
    > Any help would be very much appreciated.
    >
    > Thanks
    > Greg Rothlander




  3. #3
    Greg Rothlander Guest

    Re: Getting the attribute value of an XML Element


    That was it!

    Just needed to know to use the @ sign when you need the attribute value and
    not the element value. Why can't they write books where you can just simply
    look this stuff up? I must have went through 3 or 4 books appendix looking
    up works like attribute, value-of, etc... without any luck.

    Thanks so much...
    Greg Rothlander

    "Russell Jones" <arj1@northstate.net> wrote:
    >If I understood your question, you simply wanted to put the company name

    in
    >the table header. This should work, or at least give you a good idea of

    how
    >to proceed.
    >
    ><xsl:template match="TotalSales">
    > <table width="100%" border="1">
    > <tr><th><xsl:value-of select="@Company"/></th></tr>
    > <xsl:for-each select="DailySales/DailySalesDetail">
    > <tr><td><value-of select="Units"/></td></tr>
    > </xsl:for-each>
    > </table>
    ></xsl:template>
    >
    >Russell Jones
    >Sr. Web Development Editor,
    >DevX.com
    >
    >"Greg Rothlander" <grothlander@austin400.com> wrote in message
    >news:3b376bf0$1@news.devx.com...
    >>
    >> I have not needed to access an elements attribute value since I've started
    >> working with XML and today I need to figure it out but I'm not having

    much
    >> luck. This should be a very easy exercise but I think I'm missing

    >something
    >> very simple.
    >>
    >> Here is a sample of the stream:
    >>
    >> <TotalSales Company="Universal">
    >> <DailySales>
    >> <DailySalesDetail Date="12/01/2001">
    >> <Units>200</Units>
    >> .
    >> .
    >> .
    >>
    >> I'm using XSL to generate a table and I need to include the company name
    >> as a header and use the date attribut as part of a row in a table. I've
    >> been using the following XSL code but I'm not sure this is the best way

    to
    >> handle what I'm doing. Now that the <Company> element has become an

    >attribute
    >> of the <TotalSales> element, I'm not sure how to gets it's value out.

    All
    >> of the examples in the numerous books I have do not use the <xsl:template
    >> match="/"> like I am using it in my code. The examples I have match a

    >particular
    >> element name like <... match="DailySales" >. I've not had much luck with
    >> this method so I've been coding it as follows...
    >>
    >> <?xml version="1.0" ?>
    >> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    >> <xsl:template match="/">
    >>
    >> <xsl:for-each select="TotalSales/DailySales/DailySalesDetail">
    >> <table width="100%" border="1">
    >> <tr><td><value-of select="Units"/></td></tr>
    >> </table>
    >> </xsl:for-each>
    >> .
    >> .
    >> .
    >>
    >>
    >> How do I access the "Company" and "Date" elements in the example stream?
    >> Any help would be very much appreciated.
    >>
    >> Thanks
    >> Greg Rothlander

    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links