I have an xml datasource which looks like this:
<dsQueryResponse>
<Department>
<Rows>
<Row dept="HR" state="NY" region="East"/>
<Row dept="HR" state="NJ" region="East"/>
<Row dept="SD" state="NY" region="East"/>
<Row dept="MM" state="NY" region="East"/>
<Row dept="SD" state="NJ" region="East"/>
<Row dept="SD" state="CO" region="West"/>
<Row dept="MM" state="CO" region="West"/>
</Rows>
</Department>
</dsQueryResponse>
My XSLT looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:param name="DeptQS">East</xsl:param>
<xsl:variable name="deptRows" select="/dsQueryResponse/Department/Rows/Row[@region = $DeptQS]"/>
<xsl:template match="/">
<xsl:if test="count($deptRows) > 0">
<xsl:call-template name="deptList"/>
</xsl:if>
</xsl:template>
<xsl:template name="deptList">
<xsl:for-each select="$deptRows">
<!-- process unique depts-->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I want to get all the departments in the region written out. The code I have now will write out duplicate departments. But how can I write out the department for the region only once?
Thanks!
This stylesheet:
Output:
You haven't shown your desired output. But in XSLT 2.0, you could do something like this:
The following stylesheet shows a general approach to grouping at multiple levels:
It produces the following output on your input:
For XSLT 1.0 you can use the "Muenchian Method".
You would create a key to index the
Row
elements by a combination of@region
and@dept
values.Then you would get the first occurrence of that region/department combination (that has the wanted region (
$DeptQS
)) and output the department (dept
).Here's an example stylesheet outputting
<test>
elements to show the context:Here's the output using your input XML: