Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2010, 10:37 PM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl reordering of elements

I have a large merged dataset from c# dataset merge class as:

<message>
<block1>
<name1>bob</name1>
<field2><2</field2>
<field3>3</field3>
</block1>
<block2>
<name2>bob</name2>
<field5>2</field5>
<field6>3</field6>
</block2>
message

desired output from xsl is:
<group1>
<name1>bob</name1>
<name2>bob</name2>
</group1>
<group2>
<field2><2</field2>
<field5>2</field5>
</group2>
<group3>
<field3>3</field3>
<field6>3</field6>
</group3>

i am needing to reoder the xml and group tags based on equal attribue values
having different element paths in the same xml dataset.

thoughts?
 
Old April 1st, 2010, 03:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

In XSLT 2.0 you can use the xsl:for-each-group instruction to group on the text of the element:

Code:
   <xsl:template match="message">
   	  <xsl:for-each-group select="*/*" group-by="text()">
			      <xsl:element name="group{position()}">
					       <xsl:copy-of select="current-group()"/>
				     </xsl:element>
   		</xsl:for-each-group>
   </xsl:template>
In XSLT 1.0 (which if you are using C# then you will probably have, although both Saxon and Altova will work from within .Net) then you need to use muenchian grouping. http://www.jenitennison.com/xslt/gro...muenchian.html.

Code:
   <xsl:key name="block" match="/message/*/*" use="text()"/>
   <xsl:template match="message">
   	  <xsl:for-each select="*/*[generate-id() = generate-id(key('block', text())[1])]">
			      <xsl:element name="group{position()}">
							     <xsl:copy-of select="key('block', text())"/>
				     </xsl:element>
   		</xsl:for-each>
   	</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 2nd, 2010, 08:44 AM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Post XSL grouping across nodes

Thanks for the reply, lets say i have this issue:

<tag>
<misc1>
<matchfield>A</matchfield>
<another>1</another>
</misc1>
<misc1>
<matchfield>B</matchfield>
<another>5</another>
</misc1>
<misc2>
<matchfield>A</matchfield>
<anotherone>2</anotherone>
</misc2>
<misc2>
<matchfield>B</matchfield>
<anotherone>9</anotherone>
</misc2>
</tag>



Output needs to group nodes where value of matchfield equals: So where matchfield equals i need to group the nodes under a group tag


<tag>
<group>
<misc1>
<matchfield>A</matchfield>
<another>1</another>
</misc1>
<misc2>
<matchfield>A</matchfield>
<anotherone>2</anotherone>
</misc2>
</group>
<group>
<misc1>
<matchfield>B</matchfield>
<another>5</another>
</misc1>
<misc2>
<matchfield>B</matchfield>
<anotherone>9</anotherone>
</misc2>
</group>
</tag>


Advice would be greatly appreciated.
 
Old April 2nd, 2010, 08:50 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you use XSLT 1.0 or 2.0?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 2nd, 2010, 10:02 AM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

XSLT version 1.0

I should have specified.

Thanks.
 
Old April 2nd, 2010, 11:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Read the link posted on Muechian grouping. Here is it that technique applied to your sample:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:key name="k1" match="tag/*" use="matchfield"/>
  
  <xsl:template match="tag">
    <xsl:copy>
      <xsl:for-each select="*[generate-id() = generate-id(key('k1', matchfield)[1])]">
        <group>
          <xsl:copy-of select="key('k1', matchfield)"/>
        </group>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 2nd, 2010, 12:45 PM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that was some great info, I think i am getting real close.

How would i handle the added level to the mix where they are not named the same. When i apply the last thread result to the added tag below, i get empty results.

Please let me know what you think.



<tag>
<misc1>
<addedlevel1>
<matchfield>A</matchfield>
<another>1</another>
</addedlevel1>
</misc1>
<misc1>
<addedlevel1>
<matchfield>B</matchfield>
<another>5</another>
</addedlevel1>
</misc1>
<misc2>
<addedlevel2>
<matchfield>A</matchfield>
<anotherone>2</anotherone>
</addedlevel2>
</misc2>
<misc2>
<addedlevel2>
<matchfield>B</matchfield>
<anotherone>9</anotherone>
</addedlevel2>
</misc2>
</tag>


output expected is:

<tag>
<group>
<misc1>
<addedlevel1>
<matchfield>A</matchfield>
<another>1</another>
</addedlevel1>
</misc1>
<misc2>
<addedlevel2>
<matchfield>A</matchfield>
<anotherone>2</anotherone>
</addedlevel2>
</misc2>
</group>
<group>
<misc1>
<addedlevel1>
<matchfield>B</matchfield>
<another>5</another>
</addedlevel1>
</misc1>
<misc2>
<addedlevel2>
<matchfield>B</matchfield>
<anotherone>9</anotherone>
</addedlevel2>
</misc2>
</group>
</tag>


If you get this figured out. I so hope you have an answer to this. Your input has been great.
 
Old April 2nd, 2010, 12:51 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can use * as a wildcard for the level with different element names:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:key name="k1" match="tag/*" use="*/matchfield"/>
  
  <xsl:template match="tag">
    <xsl:copy>
      <xsl:for-each select="*[generate-id() = generate-id(key('k1', */matchfield)[1])]">
        <group>
          <xsl:copy-of select="key('k1', */matchfield)"/>
        </group>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 2nd, 2010, 01:08 PM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you had to go 2 levels would it be */*/
 
Old April 2nd, 2010, 01:23 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Yes, just try it. And read an XPath tutorial, that is fundamental to doing XSLT.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to group xsl elements in xslt.10 suji XSLT 1 February 29th, 2008 03:44 AM
how t do comparision of elements in xsl suji XSLT 2 February 28th, 2008 12:27 PM
Adding elements from an XML to XSL. AjayLuthria XSLT 1 May 1st, 2007 12:12 PM
Dynamically Finding Elements in XSL tclancy XSLT 2 March 1st, 2006 03:25 PM
Reordering Using XSL zenmaster XSLT 2 December 7th, 2005 08:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.