SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get element value from element with attributes?

    Hello,
    I need to get element value from element with attributes. I know how to get value from 'simple' element, but I don't know how to work with attributes.

    For example, my xml is

    Code:
    <s>
      <s n="MyData">
        <c n="User"></c>
        <c n="ValidationErrors"></c>
        <c n="errorCode">0</c>
        <a n="responseDate">20100809104232</a>
        <a n="streamID">23747608</a>
        <a n="requestNumber">1</a>
      </s>
    </s>
    I need to get <a> (ie, 20100809104232, 23747608 and 1).

    I wrote xsl:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/s/s">
     <table border="1" cellpadding="5" cellspacing="3">
     <tr>
      <td>Status:</td><td><xsl:apply-templates select="a" /></td>
     </tr>
    
     </table>
    </xsl:template>
    </xsl:stylesheet>
    But I get 020100809104232237476081 in one string. Help!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    If you want to get separate table rows you can wrap them in xsl:for-each
    To get only specific attributes you can use XPATH where the syntax for the select is like
    element[@attribute_name]
    or for attributes with specific name/values
    element[@attribute_name='desired_value']

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Code XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     
    <xsl:template match="/s/s">
     
    <html>
    <head>
    </head>
    <body>
     <table border="1" cellpadding="5" cellspacing="3">
     <tr>
      <td>Status:</td>
      <xsl:for-each select="a">
      	<td><xsl:value-of select="."/></td>
      </xsl:for-each>
     </tr>
     
     </table>
     </body>
     </html>
    </xsl:template>
    </xsl:stylesheet>

    Not sure exactly what the intended output presentation is but there they are in separate cells.


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
  •