Hi!
I have two problems with the below code that I need help fixing:
1) It doesn't include the root node when returning results from the XPath expression. (I've tried a couple things, but it messes up the results even more..)
2) I need help fixing the formatting of the results. I need the same nodes with the same attributes to be listed under one heading instead of having a heading for every result. I had a similar question earlier, but now that issues with the code were fixed, I can't seem to get my headings to work properly without compromising the results.
Besides the issue with missing the root node, I believe this returns the correct results, therefore I do not want to change the code drastically.
Here is some intentionally inconsistent dummy XML I am testing with:
<pets name="myPets" NUM="2">
<dog name="allMyDogs" NUM="5">
<dog name="Frank" cute="yes" color"brown" type="Lab" NUM="3"/>
<dog name="Frank" NUM="3"/>
<dog name="Spot" NUM="4"/>
<dog name="Rover" cute="yes" NUM="1"/>
<dog name="Rupert" cute="yes" type="Pug" color="black" NUM="6"/>
<cat name="Lucy" cute="yes" NUM="4"/>
</dog>
<cat name="allMyCats" NUM="4">
<cat name="Simba" cute="yes" NUM="4"/>
<cat name="Princess" cute="no" color="black" NUM="5"/>
<cat name="Fluffy" cute="yes" color="grey" NUM="1"/>
<cat name="Lucy" cute="yes" color="brown" NUM="3">
<cat name="Lucy" cute="no" NUM="35"/>
<cat name="Lucy" cute="yes" purrs="yes" NUM="6"/>
</cat>
<cat name="Lucy"cute="no" color="grey" NUM="1"/>
<dog name="Rover" cute="yes" NUM="24"/>
</cat>
<cat name="Lucy" NUM="9"/>
<dog name="Rupert Jr" cute="yes" type="Pug" color="black" NUM="0"/>
</pets>
And here is the XSLT code:
<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>
And the desired formatting for output:
Element Name Name NUM
pets myPets 2
Element Name Name Cute Color NUM Type
dog Frank yes brown 3 Lab
dog Rupert Jr yes black 0 Pug
Element Name Name NUM
dog Frank 3
dog Spot 4
Element Name Name Cute NUM
dog Rover yes 1
Element Name Name Cute NUM
cat Lucy yes 4
cat Simba yes 4
Element Name Name NUM
cat allMyCats 4
Element Name Name Color Cute NUM
cat Fluffy grey yes 1
cat Lucy brown yes 3
cat Lucy grey no 1
Thanks! :) Let me know if I need to clear anything up!