Hi!
So I have a really confusing problem on my hands..
It seems as though that the entire XML data hierarchy is not being searched through when using an XPath expression in XSL.
Some dummy XML data:
<pets name="myPets" NUM="2">
<dog name="allMyDogs" NUM="5">
<dog name="Frank" NUM="3"/>
<dog name="Spot" NUM="4"/>
<dog name="Rover" NUM="1"/>
<dog name="Rupert" NUM="6"/>
<cat name="Lucy" NUM="4"/>
</dog>
<cat name="allMyCats" NUM="4">
<cat name="Simba" NUM="4"/>
<cat name="Princess" NUM="5"/>
<cat name="Fluffy" NUM="1"/>
<cat name="Lucy" NUM="3"/>
<cat name="Lucy" NUM="35"/>
<cat name="Lucy" NUM="6"/>
<cat name="Lucy" NUM="1"/>
</cat>
<cat name="Lucy" NUM="9"/>
</pets>
The following is the portion of XSLT code I believe is causing the issue:
<xsl:key name="elem_key" match="elem" use="concat(@key, .)" />
<xsl:variable name="all_data">
<xsl:apply-templates select="*">
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:variable>
<xsl:template match="//*[@NUM<=4]">
<elem key="{name()}">
<xsl:copy-of select="@*" />
<xsl:for-each select="@*">
<xsl:sort select="name()" />
<attribute>|<xsl:value-of select="name()" />|</attribute>
</xsl:for-each>
</elem>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="msxsl:node-set($all_data)">
<xsl:for-each select="*[generate-id()=generate-id(key('elem_key',concat(@key, .))[1])]">
<table >
<tr>
<td>Element Name</td>
<xsl:for-each select="*">
<td>
<xsl:value-of select="translate(.,'|','')" />
</td>
</xsl:for-each>
</tr>
<xsl:for-each select="key('elem_key', concat(@key, .))">
<xsl:variable name="curr_elem" select="." />
<tr>
<td>
<xsl:value-of select="@key" />
</td>
<xsl:for-each select="*">
<td >
<xsl:value-of select="$curr_elem/@*[name()=translate(current(),'|','')]" />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
<p />
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
The XPath expression used:
//*[@NUM<=4]
(Above should generate many results)
The incorrect results I get:
Element Name name NUM
pets myPets 2
As you can see it seems to stop at the root.
If I change the XPath to:
//*[@NUM=4]
I get these incorrect results:
Element Name name NUM
dog Spot 4
Element Name name NUM
cat Lucy 4
Element Name name NUM
cat allMyCats 4
What appears to happen is that it will stop searching down into the hierarchy once it has found a match. The first two (Spot and Lucy) are correct, but then it stopped at allMyCats, when there is a child node of allMyCats (Simba) that has a NUM of 4.
Can anyone help me fix this code so that it returns the correct results? I'm quite frustrated! :(
Thanks!