tags:

views:

225

answers:

2

I am trying to convert some XML with XSL, to make the output look better and more readable for other users. I have some XML along the lines of:

<G>
  <OE>
    <example1>Sample 1</example1>
    <example2>Sample 2</example2>
    <var name="name1">
      <integer>1</integer>
    </var>
  </OE>
</G>

I want to get all the details from it and display it in a table so I use the following XSL code:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:template match="/">
    <html>
      <body>
        <h2>Heading</h2>
        <table border="1">
          <tr bgcolor="#3399FF">
            <th>Example 1</th>
            <th>Example 2</th>
            <th>Var name</th>
            <th>Int</th>
          </tr>
          <xsl:for-each select="G/OE">
            <xsl:for-each select="var">
              <tr>
                <td>
                  <xsl:value-of select="Example 1" />
                </td>
                <td>
                  <xsl:value-of select="Example 2" />
                </td>
                <td>
                  <xsl:value-of select="@name" />
                </td>
                <td>
                  <xsl:value-of select="integer" />
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I know that the following XSL won't work properly. I want the output to be:

Sample 1, Sample 2, name1, 1 - in a table format.

My problem is I don't know how to do this. In the above XSL code it will only retrieve var and integer. If I only use the top <xsl:for-each>, then only the first two are retrieved.

I have tried moving the 2nd <xsl:for-each> after the first to have been selected, so between:

<td><xsl:value-of select="Example 2"/></td>

and

<td><xsl:value-of select="@name"/></td>

I get an error. Is there any way to solve this problem?

Thanks for the help.

A: 

You're looping (in the inner for-each) over var, so both example tags are outside your scope at that point. You may want to remove the inner for-each loop (and refer to var/@name and var/integer instead of @name and integer).

djc
A: 

Thank you djc. Thats exactly what I needed and you have solved my issue. Thanks again!

His answer:

You're looping (in the inner for-each) over var, so both example tags are outside your scope at that point. You may want to remove the inner for-each loop (and refer to var/@name and var/integer instead of @name and integer).

@Meeeee: Please don't use Stack Overflow like a forum, since it is not a forum. Please delete this and use the comment function to express gratitude. ;-) Also, it's not necessary to quote other answers, just vote them up and accept them if they worked for you.
Tomalak