0

Need to pull data from each set of tags in xml. Right now I am only getting the first set of data for each row. Here is the XML I am pulling data from

 <message>
  <Data>
      <Name>John Doe</Name>
      <Date>2/14/2012</Date>
      <Phone>1234567</Phone>
  </Data>

   <Data>
     <Name>Jane Doe</Name>
      <Date>4/19/2012</Date>
      <Phone>2345678</Phone>
   </Data>

   <Data>
      <Name>Mike Doe</Name>
      <Date>12/14/2011</Date>
      <Phone>3456789</Phone>
   </Data>
  </message>

the XSLT I am using is this.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:output method="html" version="1.1" encoding="iso-8859-1" />

  <xsl:template match="/message">
    <html>
    <body>
    <table border="1">
    <tr>
            <th ColSpan="4">Person</th>
    </tr>
    <tr>
            <th>Name</th>
            <th>Date</th>
        <th>Phone</th>
          </tr>
    <xsl:for-each select="//Data">
      <tr>
     <td>
     <xsl:value-of select="//Name" />
      </td>
      <td>
      <xsl:value-of select="//Date" />
      </td>
      <td>
      <xsl:value-of select="//Phone" />
      </td>
      </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
</xsl:stylesheet>

My output only shows the John Doe's information on all three rows.

2 Answers 2

2
<tr>
    <td>
        <xsl:value-of select="//Name" />
    </td>
    <td>
        <xsl:value-of select="//Date" />
    </td>
    <td>
        <xsl:value-of select="//Phone" />
    </td>
</tr>

Your problem lies in here. You've selected all the Data tags and are iterating over them, but when you get the value, you're grabbing all of the Name, Date, or Phone tags that are in the document then getting the value of the first one, which is John Doe's.

<tr>
    <td>
        <xsl:value-of select="Name" />
    </td>
    <td>
        <xsl:value-of select="Date" />
    </td>
    <td>
        <xsl:value-of select="Phone" />
    </td>
</tr>

Since in the for-each, you're in the scope of the Data tag, you can just use the name to select the child node.

I would also recommend splitting that for-each out into a template for the sake of style in XSLT. So, your final transform would look like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="html" version="1.1" encoding="iso-8859-1" />
    <xsl:template match="/message">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th ColSpan="4">Person</th>
                    </tr>
                    <tr>
                        <th>Name</th>
                        <th>Date</th>
                        <th>Phone</th>
                    </tr>
                    <xsl:apply-templates select="Data"/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Data">
        <tr>
            <td>
                <xsl:value-of select="Name" />
            </td>
            <td>
                <xsl:value-of select="Date" />
            </td>
            <td>
                <xsl:value-of select="Phone" />
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Example: http://www.xsltcake.com/slices/MzgTXl

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing all of the //...

    <xsl:for-each select="Data">
        <tr>
            <td>
                <xsl:value-of select="Name" />
            </td>
            <td>
                <xsl:value-of select="Date" />
            </td>
            <td>
                <xsl:value-of select="Phone" />
            </td>
        </tr>
    </xsl:for-each>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.