user1800552
user1800552

Reputation: 372

XPath in XSLT not giving all members

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?

Upvotes: 0

Views: 102

Answers (1)

PhillyNJ
PhillyNJ

Reputation: 3902

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>

Upvotes: 1

Related Questions