0

I have XML so:

<Root>
  <ID>NSA</ID>
  <Groups>
    <Group>
      <ID>Europe</ID>
      <Levels>
        <Level>
          <RootLevelID>Cases B</RootLevelID>
          <Faults>
            <Fault>
              <FaultID>case 1</FaultID>
            </Fault>
            <Fault>
              <FaultID>case 2</FaultID>
            </Fault>
          </Faults>
        </Level>
      </Levels>
    </Group>
  </Groups>
</Root>

And I use the following XSL to make it html for the sake of readability:

<xsl:stylesheet version="1.0">
  <xsl:output omit-xml-declaration="yes" method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Output</title>
      </head>
      <body>
        <xsl:for-each select="//Root">
          <Table border="1">
            <Th>
              <xsl:value-of select="ID"/>
            </Th>
            <Tr>
              <td>
                <xsl:for-each select="current()//Group">

                  <xsl:for-each select="current()//Level">
                    <tr>
                      <td>
                        <xsl:value-of select="current()//RootLevelID"/> Level name <xsl:for-each
                          select="current()//Fault"> <td>
                            <xsl:value-of select="FaultID"/> Fault name </td> </xsl:for-each>
                      </td>
                    </tr>
                  </xsl:for-each>
                </xsl:for-each>
              </td>
            </Tr>
          </Table>
          <br/>
          <br/>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

But i will only get the first Fault-member, not all, even tho its inside for-each loop. It only outputs "case 1".

However as this is a part of the bigger context, the first 2 for-each loops (Root and Group ) iterates correctly all the group members in the xml.

Maybe the nested for-each loops are not working very well in XPATH?

2
  • Although not very elegant, the XSLT you have shown should output both "case 1" and "case 2". I have noticed though you are outputting a tr element within a td element, which seems odd. Or perhaps you are showing us some abridged XSLT, which is normally not a bad thing, unless you have chopped out a bit of a crucial bit of code that affects things significantly. Commented Dec 31, 2013 at 14:01
  • yes, it was correct, i was looking at wrong output file... Commented Dec 31, 2013 at 15:53

1 Answer 1

1

As noted by @Tim C your xslt is not elegant but does work. As a note, I am not sure why you are using current() when you can easily process the xml in document order:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Output</title>
            </head>
            <body>
                <xsl:for-each select="/Root">
                    <table border="1">
                        <th>
                            <xsl:value-of select="ID"/>
                        </th>
                        <tr>
                            <td>
                                <xsl:for-each select="Groups/Group">
                                    <xsl:for-each select="Levels/Level">
                                        <tr>
                                            <td>
                                                <xsl:value-of select="RootLevelID"/>
                                                <xsl:text> Level name</xsl:text>
                                                <xsl:for-each select="Faults/Fault">
                                                    <td>
                                                        <xsl:value-of select="FaultID"/>
                                                        <xsl:text>Fault name </xsl:text>
                                                    </td>
                                                </xsl:for-each>
                                            </td>
                                        </tr>
                                    </xsl:for-each>
                                </xsl:for-each>
                            </td>
                        </tr>
                    </table>
                    <br/>
                    <br/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

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.