I would like to categorize results from an XPath under headings by element name (and then by same attribute names). Note: XML data could be inconsistent and some elements with the same name could have different attributes, therefore they need different headings.
I can't seem to write out my problem in words, so it might be best to use an example..
XML:
<pets>
<dog name="Frank" cute="yes" color"brown" type="Lab"/>
<cat name="Fluffy" cute="yes" color="brown"/>
<cat name="Lucy" cute="no" color="brown"/>
<dog name="Spot" cute="no" color="brown"/>
<dog name="Rover" cute="yes" color="brown"/>
<dog name="Rupert" cute="yes" color="beige" type="Pug"/>
<cat name="Simba" cute="yes" color="grey"/>
<cat name="Princess" color="brown"/>
</pets>
XPath:
//*[@color='brown']
What the output should sort of look like (with the different headings for different elements):
ElementName Color Cute Name Type
Dog Brown Yes Frank Lab
ElementName Color Cute Name
Dog Brown No Spot
Dog Brown Yes Rover
ElementName Color Cute Name
Cat Brown Yes Fluffy
Cat Brown No Lucy
ElementName Color Name
Cat Brown Princess
The XSL I currently have (simplified!):
<xsl:apply-templates select="//*[@color='brown']" mode="result">
<xsl:sort select="name()" order="ascending"/>
</xsl:apply-templates>
<xsl:template match="@*|node()" mode="result">
<tr>
<th align="left">Element</th>
<xsl:for-each select="@*">
<xsl:sort select="name()" order="ascending"/>
<th align="left">
<xsl:value-of select="name()"/>
</th>
</xsl:for-each>
</tr>
<tr>
<td align="left">
<xsl:value-of select="name()"/>
</td>
<xsl:for-each select="@*">
<xsl:sort select="name()" order="ascending"/>
<td align="left">
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
</xsl:template>
This above XSL sorts them correctly in the way I want.. but now I need some sort of check to see which elements have the same name, and then if they have the same name, do they have the same attributes. Once I complete this check, I can then put general "Headings" above sets of records with matching element name and attributes.
I figured I could use xsl:choose xsl:when and do some tests. I was thinking (after the correct ordering has been done):
If element name != previous element name
create headings
Else if all attributes != all previous element's attributes
create headings
I guess my biggest problem is, is that I don't know how to check what the previous returned data set was... Can someone please tell me how to do this?
Or if I am approaching this wrong.. lead me to a better solution?
Hope that all made sense! Let me know if you need clarification!
Thanks in advance for your patience and responses! :)
This stylesheet:
Result:
Note: This assumes that all elements having the same number of attributes have also the same attribute's name (like in your input sample).
EDIT: Better ouput markup.
EDIT 2: Another kind of solution: one header with all posible attribute (like CSV pattern) and order element by attribute count and name.
Result:
Note: This runs through the tree twice but without extension. Exact match for desired output without extensions would require to mimic key mechanism like this: run through the tree adding new keys (name of element plus attributes' names) to a param, then again for every key run through the tree filtering node by key (could be a little optimization keeping a node set for non matching elements...). Worst case (every node with distinc key) will pass trough a node: N (for key building) + (N + 1) * N / 2
This transformation doesn't make any assumptions about the sets having the same number of attributes -- no assumptions at all.
When applied on the provided XML document:
the wanted, correct result is produced: