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 November 25th, 2011, 05:29 AM
Registered User
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation How to Group multiple item value in one attribute

Hello,

My input XML is look like as below.

<AnswerSet>
<Answer questionId="Type" IncludeInGroup="true">Standard</Answer>
<Answer questionId="Options" type="list" selectedValue="1" IncludeInGroup="true">item1</Answer>
<Answer questionId="Options" type="list" selectedValue="2" IncludeInGroup="true">item2</Answer>
<Answer questionId="Options" type="list" selectedValue="3" IncludeInGroup="true">item3</Answer>
<Answer questionId="name" IncludeInGroup="false">my name</Answer>
</AnswerSet>


And need output like

<nodes>
<node Type="Standard" Options="item1|item2|item3"/>
</nodes>

just check <Answer node have IncludeInGroup=True then create attribute in node with name is value of QuestionID and value of create attribute is value if that answer tag.
Also if we have QuestionID multiple times then concat it's value with | sign.

please provide sample in XSLT 1.0

Please help me out for this issue

Thanks,
Sadiq
 
Old November 25th, 2011, 08:52 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
 
<xsl:output indent="yes"/>

<xsl:key name="k1" 
   match="AnswerSet/Answer[@IncludeInGroup = 'true']"
   use="@questionId"/>
  
<xsl:template match="/">
  <nodes>
    <xsl:apply-templates/>
  </nodes>
</xsl:template>

<xsl:template match="AnswerSet">
  <node>
    <xsl:apply-templates select="Answer[@IncludeInGroup = 'true'][generate-id()= generate-id(key('k1', @questionId)[1])]" mode="group"/>
  </node>
</xsl:template>

<xsl:template match="Answer[@IncludeInGroup = 'true']" mode="group">
  <xsl:attribute name="{@questionId}">
    <xsl:apply-templates select="key('k1', @questionId)"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="Answer">
  <xsl:if test="position() &gt; 1">|</xsl:if>
  <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>
Transforms your sample
Code:
<AnswerSet>
<Answer questionId="Type" IncludeInGroup="true">Standard</Answer>
<Answer questionId="Options" type="list" selectedValue="1" IncludeInGroup="true">item1</Answer>
<Answer questionId="Options" type="list" selectedValue="2" IncludeInGroup="true">item2</Answer>
<Answer questionId="Options" type="list" selectedValue="3" IncludeInGroup="true">item3</Answer>
<Answer questionId="name" IncludeInGroup="false">my name</Answer>
</AnswerSet>
into
Code:
<nodes>
   <node Type="Standard" Options="item1|item2|item3"/>
</nodes>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Look up an ancestors attribute from a for-each-group bonekrusher XSLT 5 July 13th, 2009 12:08 PM
Extracting a number range from tag attribute group maikm XSLT 6 August 25th, 2008 04:54 PM
Accessing previous item in current-group() in loop mikeymikey XSLT 1 January 4th, 2008 06:47 AM
Group XML by Attribute values? NotesSensei XSLT 4 July 14th, 2004 11:53 AM
Find Out First Item of each item Group Jane SQL Language 1 November 22nd, 2003 12:42 PM





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