Multiple group using Muenchian Method [modified]
-
I have the following input XML. I want to group all unique Code/ID combination.
<List>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<Section>
<Code>CC</Code>
<ID>11</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>22</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<List>So result should be as shown below.
<Code>AA</Code>
<ID>11</ID><Code>CC</Code>
<ID>11</ID><Code>AA</Code>
<ID>22</ID>Im using the Muenchian Method. Currely my XSL (below) is grouping by Code (and not Code/ID combination)
How do i get this to use a Code/ID combination? I tried nested for-each loop but no luck. Thanks
modified on Tuesday, February 23, 2010 11:40 AM
-
I have the following input XML. I want to group all unique Code/ID combination.
<List>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<Section>
<Code>CC</Code>
<ID>11</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>22</ID>
<Section>
<Section>
<Code>AA</Code>
<ID>11</ID>
<Section>
<List>So result should be as shown below.
<Code>AA</Code>
<ID>11</ID><Code>CC</Code>
<ID>11</ID><Code>AA</Code>
<ID>22</ID>Im using the Muenchian Method. Currely my XSL (below) is grouping by Code (and not Code/ID combination)
How do i get this to use a Code/ID combination? I tried nested for-each loop but no luck. Thanks
modified on Tuesday, February 23, 2010 11:40 AM
Firstly, your key needs to have an expression which reflects what you want to retrieve:
<xsl:key name="CCids" match="/List/Section" use="concat(Code,'|',ID)"/>
And then you need to use that same expression when accessing items referenced by the key. Oh - and in addition, I would suggest not using a for loop - it's not idiomatic XSL. Here's the file I'd use:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns0="http://A4C.Interface.HRP.Payroll.Schemas">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="utf-8"/><xsl:key name="CCids" match="/List/Section" use="concat(Code,'|',ID)"/>
<xsl:template match="/">
ns0:List
<xsl:apply-templates select="/List/Section [generate-id(.)=generate-id(key('CCids',concat(Code,'|',ID)))]"/>
</ns0:List>
</xsl:template><xsl:template match="Section">
<Section>
<Code>
<xsl:value-of select="Code/text()" />
</Code>
<ID>
<xsl:value-of select="ID/text()" />
</ID>
</Section>
</xsl:template>
</xsl:stylesheet>Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
Firstly, your key needs to have an expression which reflects what you want to retrieve:
<xsl:key name="CCids" match="/List/Section" use="concat(Code,'|',ID)"/>
And then you need to use that same expression when accessing items referenced by the key. Oh - and in addition, I would suggest not using a for loop - it's not idiomatic XSL. Here's the file I'd use:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns0="http://A4C.Interface.HRP.Payroll.Schemas">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="utf-8"/><xsl:key name="CCids" match="/List/Section" use="concat(Code,'|',ID)"/>
<xsl:template match="/">
ns0:List
<xsl:apply-templates select="/List/Section [generate-id(.)=generate-id(key('CCids',concat(Code,'|',ID)))]"/>
</ns0:List>
</xsl:template><xsl:template match="Section">
<Section>
<Code>
<xsl:value-of select="Code/text()" />
</Code>
<ID>
<xsl:value-of select="ID/text()" />
</ID>
</Section>
</xsl:template>
</xsl:stylesheet>Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
Thanks..Really helpful ;)